Django DetailView按外键过滤 [英] Django DetailView Filter by Foreign Key
问题描述
class Category(models.Model):
name = models.CharField(max_length = 30)
slug = models.SlugField(help_text =类别的简称)
def __unicode __(self):
return self.name
class Meta:
order = [-name]
verbose_name_plural =categories
class Publisher(models.Model):
name = models.CharField(max_length = 30)
slug = models.SlugField(help_text =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 =书的简称)
pub_date = models.DateField()
publisher = models.ForeignKey (Publisher)
category = models.ForeignKey(Category)
c
order = [-title]
def __unicode __(self):
return self.title
我的Urls.py:
url(r'^类别/(?P< slug> [\w - ] +)/ $',DetailView.as_view(model = Category,template_name =books / category_detail)),
/ pre>
我的Category_detail.html
{%block content% }
< h2> {{category.name}}< / h2>
< ul>
< li>书名:{{book.title}}< / li>
< li>图书出版商:{{book.publisher}}< / li>
< li>图书发布日期:{{book.pub_date}}< / li>
< / ul>
{%endblock%}
基本上我想显示在我的category_detail.html以下信息:
任何帮助将不胜感激。
谢谢 - Keoko
感谢您的回复。我创建了一个具有以下信息的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 =查询数据库中的所有对象
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 '^类别/(P<蛞蝓> [\w - ] +)/ $' ,BooksCategoryListView.as_view()),
)
最后修改了category_detail.html with以下内容:
{%block content%}
< h2>图书详细信息< / h2>
< ul>
< li>类别:{{book.category}}< / li>
< li>标题:{{book.title}}< / li>
< li>作者:{{book.authors}}< / li>
< li>发布者:{{book.publisher}}< / li>
< li>发布日期:{{book.pub_date}}< / li>
< / ul>
{%endblock%}
I am little puzzled and would like to utilized the DetailView functionality using the foreign key as my filter to display the data. Basically my model looks like this:
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
My Urls.py:
url(r'^categories/(?P<slug>[\w-]+)/$', DetailView.as_view(model=Category,template_name="books/category_detail" )),
My 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 %}
Basically I would like to display in my category_detail.html the following information:
Any help would be appreciated.
Thank you - Keoko
Thank you for your kind response. I created a views.py file with the following information:
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)
And updated my application 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()),
)
And finally modified the category_detail.html with the following:
{% 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 %}
这篇关于Django DetailView按外键过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!