将多个模型与基于 Class 的视图和分页一起使用
Using Multiple models with Class Based Views and pagination
我有 2 个型号类别和产品。
一个类别有多个产品,一个产品可以在多个类别中。
类别对自身有 FK。
`class Product(models.Model):
categories = models.ManyToManyField(Category, related_name='products')
class Category(models.Model):
parent = models.ForeignKey('self', blank=True, null=True, verbose_name='parent category', on_delete=models.CASCADE)`
我需要的模板:
- 类别数据及其children/subcategories数据
- 类别和子类别中的产品
- 产品分页
我正在使用基于 Class 的视图,但我不知道将哪个用作 class 的 model
属性,因为:
- 如果我使用类别 a,我不知道如何按产品而不是按类别设置分页
- 如果我使用产品,我需要从 url 中获取类别 slug/id 并按类别过滤产品查询集,并将它们传递给上下文
- 如果我使用 RawQuerySet,我会得到一个错误
RawQuerySet' has no len()
您应该将模型设置为 Product
,因为您实际上是在该模型上查看和分页的。您的视图 class 应该类似于以下答案:
型号:
class Product(models.Model):
categories = models.ManyToManyField(Category, related_name='products')
class Category(models.Model):
parent = models.ForeignKey('self', blank=True, null=True, verbose_name='parent category', related_name='children', on_delete=models.CASCADE)
您还需要更改 get_queryset
以过滤掉类别 PK。我认为它应该是这样的:
def get_children_recursive(parent_category):
children = parent_category.children.all() # This depends on adding related_name to Category
for child in children:
children += get_children_recursive(child)
return children
class ProductListView(ListView):
paginate_by = 10
template_name = 'templates/your_product_view.html'
model = Product
context_object_name = 'products'
def get_queryset(self):
category = Category.objects.get(id=self.kwargs['category_id'])
all_children = get_children_recursive(category)
return Product.objects.filter(categories=all_children)
category_id
应该来自 URL 并且在您的 URLs 文件中应该看起来像这样:
urlpatterns = patterns('',
(r'^products/(?P<category_id>\d+)/$', ProductListView.as_view()),
)
我有 2 个型号类别和产品。
一个类别有多个产品,一个产品可以在多个类别中。 类别对自身有 FK。
`class Product(models.Model):
categories = models.ManyToManyField(Category, related_name='products')
class Category(models.Model):
parent = models.ForeignKey('self', blank=True, null=True, verbose_name='parent category', on_delete=models.CASCADE)`
我需要的模板:
- 类别数据及其children/subcategories数据
- 类别和子类别中的产品
- 产品分页
我正在使用基于 Class 的视图,但我不知道将哪个用作 class 的 model
属性,因为:
- 如果我使用类别 a,我不知道如何按产品而不是按类别设置分页
- 如果我使用产品,我需要从 url 中获取类别 slug/id 并按类别过滤产品查询集,并将它们传递给上下文
- 如果我使用 RawQuerySet,我会得到一个错误
RawQuerySet' has no len()
您应该将模型设置为 Product
,因为您实际上是在该模型上查看和分页的。您的视图 class 应该类似于以下答案:
型号:
class Product(models.Model):
categories = models.ManyToManyField(Category, related_name='products')
class Category(models.Model):
parent = models.ForeignKey('self', blank=True, null=True, verbose_name='parent category', related_name='children', on_delete=models.CASCADE)
您还需要更改 get_queryset
以过滤掉类别 PK。我认为它应该是这样的:
def get_children_recursive(parent_category):
children = parent_category.children.all() # This depends on adding related_name to Category
for child in children:
children += get_children_recursive(child)
return children
class ProductListView(ListView):
paginate_by = 10
template_name = 'templates/your_product_view.html'
model = Product
context_object_name = 'products'
def get_queryset(self):
category = Category.objects.get(id=self.kwargs['category_id'])
all_children = get_children_recursive(category)
return Product.objects.filter(categories=all_children)
category_id
应该来自 URL 并且在您的 URLs 文件中应该看起来像这样:
urlpatterns = patterns('',
(r'^products/(?P<category_id>\d+)/$', ProductListView.as_view()),
)