我对 Django 有点陌生,所以请耐心等待。我的应用程序中有三个模型:公司、文章和成绩单。Articles 和 Transcripts 都有一个foreignkey() 字段链接到公司模型。在我的主要视图中,我列出了所有公司,但我想指出是否有每家公司的文章或成绩单。
以下是模型:
class Company(models.Model):
stock_symbol = models.CharField(max_length=5, unique=False)
company_name = models.CharField(max_length=200)
address = models.CharField(max_length=300, blank=True)
website = models.URLField(max_length=200, blank=True)
following = models.BooleanField()
class Articles(models.Model):
company = models.ForeignKey('Company', related_name='articles')
headline = models.CharField(max_length=200)
url = models.URLField(max_length=300, blank=True)
class Transcripts(models.Model):
company = models.ForeignKey('Company', related_name='transcripts')
headline = models.CharField(max_length=200)
url = models.URLField(max_length=300, blank=True)
下面是单个公司的视图,但是由于我要返回所有公司,因此显然不能使用主键。我将如何获取每个公司的这些信息?我是否以正确的方式设置模型?
class CompanyDetails(generic.DetailView):
model = Company
template_name = 'company_details.html'
def get_context_data(self, **kwargs):
pk = self.kwargs.get('pk')
context = super(CompanyDetails, self).get_context_data(**kwargs)
context['company'] = Company.objects.filter(id=pk)
context['articles'] = Articles.objects.filter(company_id=pk).order_by('-date')
context['transcripts'] = Transcripts.objects.filter(company_id=pk).order_by('-date')
return context
更新
如果我想在列中显示每个公司至少有一篇文章,我该如何更新模板?当我在答案中尝试 for 循环时,它为每篇文章创建了一个列,因此我尝试将其更改为以下内容,但每条记录都显示为“空白”。
<tbody>
{% for company in all_companies %}
<tr>
<td nowrap="true"><a href="{% url 'company_details' pk=company.pk %}">{{ company.company_name }}</a></td>
<td nowrap="true">{{ company.stock_symbol }}</td>
{% if company.following %}
<td align="center"><span class="glyphicon glyphicon-star"></span></td>
{% else %}
<td></td>
{% endif %}
<!--HAS ARTICLE(S) FLAG -->
{% if article in company.articles.all %}
<td align="center"><span class="glyphicon glyphicon-list-alt"></span></td>
{% else %}
<td>Blank</td>
{% endif %}
<td nowrap="true"><a href="{{ company.website }}" target="_blank">{{ company.website }}</a></td>
<td nowrap="true">{{ company.address }}</td>
</tr>
{% endfor %}
</tbody>
在模板中,您可以将外键从公司反向跟踪到其文章或成绩单。
{{ company }}
{% for company in company.articles.all %}
{{ article }}
{% endfor %}
如果您在详细视图中使用它,则不必在方法中设置articles
或。您根本不应该设置- 详细视图会为您执行此操作。transcripts
get_context_data
context['company']
在列表视图中,您可以循环浏览公司并以相同的方式访问文章和成绩单。
{% for company in company_list %}
{{ company }}
{% for company in company.articles.all %}
{{ article }}
{% endfor %}
{% endfor %}
这将导致对查询集中的每个公司进行查询。您可以使用prefetch_related
.
Company.objects.prefetch_related('articles', 'transcripts')
在您的列表视图中,您可以prefetch_related
通过设置使用queryset
:
class CompanyList(generic.ListView):
# No need to set model now that you have set queryset
queryset = Company.objects.prefetch_related('articles', 'transcripts')
如果您只想知道是否有关联的文章或交易,那么您不必预取它们。您可以annotate
改为使用查询集:
from django.db.models import Count
# in your view
queryset = Company.objects.annotate(num_articles=Count('article')
然后在您的模板中,您可以访问company.num_articles
.
{% if company.num_articles %}
Company has articles
{% endif %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句