2012-12-30 100 views
2

我有點困惑,並希望利用DetailView功能使用外鍵作爲我的過濾器來顯示數據。基本上我的模式是這樣的:Django DetailView按國外鍵篩選

class Category(models.Model): 
    name = models.CharField(max_length=30) 
    slug = models.SlugField(help_text="A short name for Category") 

    def __unicode__(self): 
     return self.name 

    class Meta: 
     ordering = ["-name"] 
     verbose_name_plural = "categories" 


class Publisher(models.Model): 
    name = models.CharField(max_length=30) 
    slug = models.SlugField(help_text="A short name for Publisher") 

    class Meta: 
     ordering = ["-name"] 

    def __unicode__(self): 
     return self.name 


class Book(models.Model): 
    title = models.CharField(max_length=100) 
    slug = models.SlugField(help_text="A short name for book") 
    pub_date = models.DateField() 
    publisher = models.ForeignKey(Publisher) 
    category = models.ForeignKey(Category) 

    class Meta: 
     ordering = ["-title"] 

    def __unicode__(self): 
     return self.title 

我Urls.py:

url(r'^categories/(?P<slug>[\w-]+)/$', DetailView.as_view(model=Category,template_name="books/category_detail")), 

我Category_detail.html

{% block content %} 
    <h2>{{ category.name}} </h2> 
    <ul>  
    <li>Book Title: {{ book.title }}</li> 
<li>Book publisher: {{ book.publisher }}</li> 
    <li>Book Published Date: {{ book.pub_date }}</li>  
    </ul> 
{% endblock %} 

基本上我想在我的category_detail.html顯示以下信息:

  • 類別名稱
  • 書名
  • 出版商名稱
  • 發佈日期
  • 任何幫助,將不勝感激。

    謝謝你 - Keoko

    回答

    1

    在模板中你有類別對象。你可以遍歷所有的書籍;

    {% for book in category.book_set.all %} 
        Book Title: {{ book.title }} 
        Book publisher: {{ book.publisher }} 
        Book Published Date: {{ book.pub_date }} 
    {% endfor %} 
    

    或只是第一本書;

    Book Title: {{ category.book_set.all.0.title }} 
        Book publisher: {{ category.book_set.all.0.publisher }} 
        Book Published Date: {{ category.book_set.all.0.pub_date }} 
    
    2

    謝謝你的迴應。我創建了以下信息的views.py文件:

    from django.shortcuts import get_object_or_404 
    from django.views.generic import ListView 
    from mysite.books.models import * 
    
    class BooksCategoryListView(ListView): 
    
        context_object_name = "book_list" 
    
        "get_queryset = query all the objects in the database" 
        def get_queryset(self): 
         category_slug = get_object_or_404(Category, slug=self.kwargs['slug']) 
         return Book.objects.filter(category=category_slug) 
    

    並更新我的應用程序urls.py:

    from django.conf.urls import patterns, url, include 
    from django.views.generic import ListView, DetailView 
    from mysite.books.views import BooksCategoryListView 
    from mysite.books.models import * 
    
    urlpatterns = patterns('', 
        ...snip....   
        url(r'^categories/(?P<slug>[\w-]+)/$', BooksCategoryListView.as_view()), 
    ) 
    

    最後修改用以下category_detail.html:

    {% block content %} 
    <h2>Book Details</h2> 
    <ul>  
        <li>Category: {{ book.category}}</li> 
        <li>Title: {{ book.title }}</li> 
        <li>Author: {{ book.authors }}</li> 
        <li>Publisher: {{ book.publisher }}</li> 
        <li>Published Date: {{ book.pub_date }}</li>    
    </ul> 
    {% endblock %}