在Django模板中循环关联模型的子级 [英] Loop over related model's children in Django template

本文介绍了在Django模板中循环关联模型的子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公司的模型。
然后我有一个用于公司职位的基本模型。它包含常见的帖子属性。属性是发布帖子的公司。它使用ForeignKey引用公司模型。
最后,我有一个用于A型帖子的子模型(基于CompanyPost基本模型):

 类Company (models.Model):
名称= models.CharField(...)
...


class CompanyPost(models.Model):
company = models.ForeignKey(Company,...)
...


class PostA(CompanyPost):
名称= ...

在一个模板中,我想循环显示特定公司发布的A型帖子。

我尝试了以下变体:



1)

  {%for company.companyposts_set.all.postA_set.all%中的postA 
...

2 )

  {%为company.companypost_set.all%中的公司帖子} 
{%为companyposts.postA_set中的postA。全部%}
...
{%endfor%} {%endfor%}

我尝试了上述其他子变量。

我知道我可以轻松地在视图中准备集合,例如:

  postsA = PostA.objects.filter(company__pk = pk)

并将postsA传递到模板上下文,但是我想知道是否有一种方法可以在模板中循环使用相关模型的子级。

(注意:循环使用companyposts是可行的。但是我当然可以得到所有类型的信息,例如postB等。 :

  {%for company.companypost_set.all%} 

这就是为什么我尝试上面的变体2)再次遍历结果。)

谢谢您。



更新:
谢谢大家的回答。
我知道,通过选择模型继承,我选择了一个复杂的解决方案。
在本文中,我问是否可以在模板中显示相关模型的孩子。为了不混淆问题,请在此问题我解释了为什么我使用具体的模型继承并询问什么是更好的解决方案。

解决方案

如果您不想要要在视图中定义它,可以将其定义为 Company 对象的属性。

  @property 
def post_a_set(self):
返回PostA.objects.filter(company__pk = self.pk)
post_type 选择字段的其他 CompanyPost 对象,如果不适当,其他字段为null或空白;或 post_type 字段,仅适用于该类型的数据存储为JSON字符串或JSONField(如果使用Postgresql,则存储为JSONField)。


I have a model for a company. Then I have a base model for company posts. It contains common posts attributes. An attribute is the company that publishes the posts. It refers to the Company model with a ForeignKey. Finally I have a child model (based on the CompanyPost base model) for posts of type A:

class Company(models.Model):
    name = models.CharField(...)
    ...


class CompanyPost(models.Model):
    company = models.ForeignKey(Company,...)
    ...


class PostA(CompanyPost):
    name = ...

In a template I want to loop over posts of type A published by a specific company.
I tried these variants:

1)

{% for postA in company.companyposts_set.all.postA_set.all %}
...

2)

{% for companyposts in company.companypost_set.all %}
{% for postA in companyposts.postA_set.all %}
...
{% endfor %}{% endfor %}

I tried other sub-variants of the above. None seems to work.
I know that I can easily prepare the set in the view, like:

postsA = PostA.objects.filter(company__pk=pk)

And pass postsA to the template context, but I'm wondering whether there is a way to loop over related models' children in the template.
(note: looping over companyposts works. But I get of course all types of posts, like postB etc.:

{% for post in company.companypost_set.all %}

That is why I tried variant 2) above to loop again over the results.)
Thank you in advance.

UPDATE: Thank you all for your answers. I understand that, by choosing model inheritance, I chose a convoluted solution. In the present post I'm asking whether displaying related model's children in a template is possible. In order not to confuse questions, in this question I explain why I used concrete model inheritance and ask what would be a better solution.

解决方案

If you don't want to define it in the views, you could define it as a property of Company objects.

@property
def post_a_set(self):
    return PostA.objects.filter(company__pk=self.pk)

I'm pretty sure it's the model inheritance that is causing the problems, and dimly remember seeing something like his documented. I'd echo, do you really need concrete model inheritance here? Other approaches are wider CompanyPost objects with a post_type choices field and other fields null or blank if inappropriate; or a post_type field and the data that applies only to that type stored as a JSON string or (if you are using Postgresql) a JSONField.

这篇关于在Django模板中循环关联模型的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆