我有點困惑,並希望利用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