具有多个外键的 Django 模型
Django model with multiple foreign keys
我想创建一个按区域、需求和产品类别分类的产品列表。我创建了 3 个相互回复的独立模型,因此用户可以轻松地从下拉列表区域和需要中添加类别和 select。
目前,我有 3 个区域,每个区域下都有我的需求。我想要的是呈现 Area
模型中的项目列表,在 Area
模型中的每个项目下,对 Need
模型中的所有项目进行建模,在 Need
模型中的每个项目下,对所有项目进行建模来自 Product
模型(仅标题)。
ProductCategory
模型在这里没有使用,但我在其他地方需要它,它是产品和 areas/needs 之间的连接器。添加新产品时,定义类别并仅从下拉列表中 select 它们要容易得多。
我需要创建一个循环来显示类似上面的内容。我知道这行不通,但我想展示其中的逻辑。
我的问题是渲染这样的东西的最佳方法是什么?
如何在我的 class 和 html 中获得 category_area
和 category_need
?
models.py
class Area(models.Model):
title = models.CharField(max_length=75, blank=False)
body = models.CharField(max_length=150, default='-', blank=False)
publish = models.DateTimeField('publish', default=timezone.now)
class Need(models.Model):
title = models.CharField(max_length=75, blank=False, null=False, help_text='max 75 characters')
body = models.CharField(max_length=150, default='-', blank=False)
publish = models.DateTimeField(default=timezone.now)
need_area = models.ForeignKey(Area, on_delete=models.CASCADE, related_name='need_area')
class ProductCategory(models.Model):
title = models.CharField(max_length=400, blank=False, null=False, help_text='max 400 characters')
body = models.TextField(default='-')
publish = models.DateTimeField('publish', default=timezone.now)
category_area = models.ForeignKey(Area, on_delete=models.CASCADE, related_name='category_area', null=True)
category_need = models.ForeignKey(Need, on_delete=models.CASCADE, related_name='category_need', null=True)
class Product(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=400, blank=False)
category = models.ForeignKey(ProductCategory, on_delete = models.CASCADE, blank=True, related_name='products')
def get_absolute_url(self):
return reverse("product", kwargs={'slug': self.slug})
views.py
class SearchProducts(LoginRequiredMixin, ListView):
login_url = 'login'
redirect_field_name = 'login'
template_name = 'hubble/search/manual_search.html'
model = Product
queryset = Product.objects.all()
def get_context_data(self, **kwargs):
context = super(SearchProducts, self).get_context_data(**kwargs)
product_results = ProductCategory.objects.prefetch_related("products").all()
context['product_results'] = product_results
return context
product_search.html
<div>
{% for area in product_results %}
<h6>{{area.title}}</h6>
{% endfor %}
</div>
您可以prefetch_related
所有向后关系并进行类似于此的迭代:
areas = Area.objects.prefetch_related('need_area__category_need__products')
for area in areas:
# iterate through each instance of Area
for need in area.need_area.all():
# iterate through each instance of Need for the given area
for product_category in need.category_need.all():
# iterate through each instance of ProductCategory for the given need
for product in product_category.products.all():
# iterate through each instance of product for the given product category
我想创建一个按区域、需求和产品类别分类的产品列表。我创建了 3 个相互回复的独立模型,因此用户可以轻松地从下拉列表区域和需要中添加类别和 select。
目前,我有 3 个区域,每个区域下都有我的需求。我想要的是呈现 Area
模型中的项目列表,在 Area
模型中的每个项目下,对 Need
模型中的所有项目进行建模,在 Need
模型中的每个项目下,对所有项目进行建模来自 Product
模型(仅标题)。
ProductCategory
模型在这里没有使用,但我在其他地方需要它,它是产品和 areas/needs 之间的连接器。添加新产品时,定义类别并仅从下拉列表中 select 它们要容易得多。
我需要创建一个循环来显示类似上面的内容。我知道这行不通,但我想展示其中的逻辑。
我的问题是渲染这样的东西的最佳方法是什么?
如何在我的 class 和 html 中获得 category_area
和 category_need
?
models.py
class Area(models.Model):
title = models.CharField(max_length=75, blank=False)
body = models.CharField(max_length=150, default='-', blank=False)
publish = models.DateTimeField('publish', default=timezone.now)
class Need(models.Model):
title = models.CharField(max_length=75, blank=False, null=False, help_text='max 75 characters')
body = models.CharField(max_length=150, default='-', blank=False)
publish = models.DateTimeField(default=timezone.now)
need_area = models.ForeignKey(Area, on_delete=models.CASCADE, related_name='need_area')
class ProductCategory(models.Model):
title = models.CharField(max_length=400, blank=False, null=False, help_text='max 400 characters')
body = models.TextField(default='-')
publish = models.DateTimeField('publish', default=timezone.now)
category_area = models.ForeignKey(Area, on_delete=models.CASCADE, related_name='category_area', null=True)
category_need = models.ForeignKey(Need, on_delete=models.CASCADE, related_name='category_need', null=True)
class Product(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=400, blank=False)
category = models.ForeignKey(ProductCategory, on_delete = models.CASCADE, blank=True, related_name='products')
def get_absolute_url(self):
return reverse("product", kwargs={'slug': self.slug})
views.py
class SearchProducts(LoginRequiredMixin, ListView):
login_url = 'login'
redirect_field_name = 'login'
template_name = 'hubble/search/manual_search.html'
model = Product
queryset = Product.objects.all()
def get_context_data(self, **kwargs):
context = super(SearchProducts, self).get_context_data(**kwargs)
product_results = ProductCategory.objects.prefetch_related("products").all()
context['product_results'] = product_results
return context
product_search.html
<div>
{% for area in product_results %}
<h6>{{area.title}}</h6>
{% endfor %}
</div>
您可以prefetch_related
所有向后关系并进行类似于此的迭代:
areas = Area.objects.prefetch_related('need_area__category_need__products')
for area in areas:
# iterate through each instance of Area
for need in area.need_area.all():
# iterate through each instance of Need for the given area
for product_category in need.category_need.all():
# iterate through each instance of ProductCategory for the given need
for product in product_category.products.all():
# iterate through each instance of product for the given product category