我正在Django 2.2中工作,试图为数据库构建视图,该视图将特定公司的所有信息(所有商店的位置和公司注释)编译到一个视图中。我已经在几种不同的答案中尝试过方法,但是似乎仍然无法从相关外键获取数据以显示在模板中。
models.py
class Company(models.Model):
name = models.CharField(max_length=30, primary_key=True)
official_name = models.CharField(max_length=50)
corporate_address1 = models.CharField(max_length=50)
corporate_address2 = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.ForeignKey('Country', on_delete=models.CASCADE)
slug = models.SlugField(max_length=30, unique=True)
def __str__(self):
return self.name
class Stores(models.Model):
store_name = models.CharField(max_length=30, primary_key=True)
owner = models.ForeignKey('Company', on_delete=models.CASCADE)
store_address1 = models.CharField(max_length=50)
store_address2 = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.ForeignKey('Country', on_delete=models.CASCADE)
type = models.CharField(max_length=30,choices=store_types)
slug = models.SlugField(max_length=30, unique=True)
def get_absolute_url(self):
return reverse("store-detail", kwargs={"slug": self.slug})
class notes(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
posted = models.DateTimeField(db_index=True, auto_now_add=True)
category = models.ForeignKey('Company', on_delete=models.CASCADE)
active = models.BooleanField(default=True)
def get_absolute_url(self):
return reverse("article-detail", kwargs={"id": self.id})
class Country(models.Model):
country = models.CharField(max_length=30,choices=countries,primary_key=True)
class Meta:
ordering = ["-country"]
db_table = 'country'
def __str__(self):
return self.country
views.py
class CompanyOverView(LoginRequiredMixin, DetailView):
model = Company
template_name = "company-overview.html"
slug_url_kwarg = 'slug'
query_pk_and_slug = True
pk_url_kwarg = "company.name"
模板
<div align="center">
<p>{{ object.name }}<br>({{ object.official_name }})</p>
<p>{{ object.corporate_address1 }}<br>{{ object.corporate_address2 }}<br>
{{ object.city }}<br>{{ object.state_province }}<br>
{{ object.country }}</p>
</div>
<p>List of Stores</p>
<p>
{% for instance in object_list %}
{{ instance.company.stores.store_name }}
{{ instance.company.stores.store_address1 }}
{{ instance.company.stores.store_address2 }}
{{ instance.company.stores.city }}
{{ instance.company.stores.state_province }}
{{ instance.company.stores.country }}
{% endfor %}
</p>
<p>Notes</p>
<p>
{% for instance in object_list %}
{{ instance.company.notes.title }}
{{ instance.company.notes.posted }}
{% endfor %}
</p>
使用上述代码,输入公司名称时唯一出现的是顶部的所有内容(例如,“ object.name”在页面上显示为“ Acme Corporation”)。 for循环中的任何内容都不会显示在网页上。
查看文档,除非指定,否则object_list是上下文的默认名称。我尝试了不同的组合,例如“ for company.store_set.all中的store_name”和我在其他帖子中找到的其他组合,但没有一个起作用。我已经整理了与外键相关的所有文档,但是找不到有效的解决方案。
如果能提供帮助,请先感谢。
答案 0 :(得分:0)
不。 object_list
是ListView中的默认上下文名称。但是您有一个DetailView,并且您已经知道默认的上下文名称是什么,因为您已经在使用它:object
。您只需要从那里迭代反向关系:
{% for store in object.stores_set.all %}
{{ store.store_name }}
{{ store.store_address1 }}
...
{% endfor %}
答案 1 :(得分:-1)
尝试在从商店到公司的外键上设置一个related_name:
class Stores(models.Model): # It's better to name this model 'Store' singular.
...
owner = models.ForeignKey('Company', on_delete=models.CASCADE, related_name='company_store') # Choose a related name that makes sense and does not conflict with other related names or models.
...
运行您的迁移。现在,您应该可以使用相关名称浏览商店:
{% for instance in object_list %}
...
{% for store in instance.company_store.all %}
{{ store.store_name }}
{{ store.store_address1 }}
{{ store.store_address2 }}
{{ store.city }}
{{ store.state_province }}
{{ store.country }}
{% endfor %}
...
{% endfor %}
这样,您可以循环浏览每个Company实例的相关存储。希望这可以帮助。让我知道您是否遇到麻烦。