了解Django,并使用Django书来跟踪探戈,但最后一个问题在添加子类别之后得到了我,该子类别未包含在该教程中。
我有以下内容:
models.py
class Category(models.Model):
"""Category"""
name = models.CharField(max_length=50)
slug = models.SlugField()
def save(self, *args, **kwargs):
#self.slug = slugify(self.name)
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
class SubCategory(models.Model):
"""Sub Category"""
category = models.ForeignKey(Category)
name = models.CharField(max_length=50)
slug = models.SlugField()
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(SubCategory, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
和
urls.py
(r'^links/$', 'rango.views.links'),
(r'^links/(?P<category_name_slug>[\w\-]+)/$', 'rango.views.category'),
(r'^links/(?P<category_name_slug>[\w\-]+)/(?P<subcategory_name_slug>[\w\-]+)/$', 'rango.views.subcategory'),
和
views.py
@require_GET
def links(request):
"""Linkdirectory Page"""
category_list = Category.objects.order_by('name')
context_dict = {'categories': category_list}
return render(request, 'links.html', context_dict)
@require_GET
def category(request, category_name_slug):
"""Category Page"""
category = Category.objects.get(slug=category_name_slug)
subcategory_list = SubCategory.objects.filter(category=category)
context_dict = {'subcategories': subcategory_list}
return render(request, 'category.html', context_dict)
@require_GET
def subcategory(request, subcategory_name_slug, category_name_slug):
"""SubCategory Page"""
context_dict = {}
try:
subcategory = SubCategory.objects.get(slug=subcategory_name_slug)
context_dict['subcategory_name'] = subcategory.name
websites = Website.objects.filter(sub_categories=subcategory)
context_dict['websites'] = websites
context_dict['subcategory'] = subcategory
except SubCategory.DoesNotExist:
return render(request, 'subcategory.html', context_dict)
这一切都很有效,直到我添加具有相同名称的子类别,例如子类别&#34;其他&#34;适用于多个类别。
我理解为什么,当我到达&#34; def subategory&#34;我的slug将返回多个子类别,因此我需要以某种方式将这些限制为相关类别,例如
"SELECT
subcategory = SubCategory.objects.get(slug=subcategory_name_slug)
WHERE
subcategory = SubCategory.objects.filter(category=subcategory)
CLAUSE"
或者什么;)
不确定最佳路线是什么,以及如何过滤这些
答案 0 :(得分:3)
鉴于您可能为两个不同的SubCategory
对象设置了两个具有相同名称的Category
个对象,如您所建议的那样,您可以添加Category
作为附加过滤器。
为了达到这个目的,我看到你有一个视图,它为SubCategory
和Category
设置了slu,你定义的像这样subcategory(request, subcategory_name_slug, category_name_slug)
。这些就足以过滤:
subcategory = SubCategory.objects.get(
slug=subcategory_name_slug,
category__slug=category_name_slug
)
^
|__ # This "double" underscore category__slug is a way to filter
# a related object (SubCategory.category)
# So effectively it's like filtering for SubCategory objects where
# SubCategory.category.slug is category_name_slug
您在上面看到我使用SubCateogry.objects.get(...)
来获取单个对象而不是可以返回许多对象的`SubCategory.objects.filter(...)。
要使用get()
安全地执行此操作,需要保证对于任何给定的类别,不会超过一个具有相同名称的子类别
您可以使用unique_together
class SubCategory(models.Model):
class Meta:
unique_together = (
('category', 'name'), # since slug is based on name,
# we are sure slug will be unique too
)