[英]ManytoMany field django template when models are in different apps
My question is similar to this . 我的问题与此类似。 I have 2 models linked with m2m field and I want to render the field in template. 我有2个与m2m字段链接的模型,我想在模板中渲染该字段。 How can I do this when my 2 models are in different apps: 当我的2个模型在不同的应用程序中时,我该怎么做:
apps/qapp/models
class Area(models.Model):
name = models.CharField(max_length=100, primary_key=True)
def __unicode__(self):
return self.name
apps/worksheets/models
class Place(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100, primary_key=True)
area = models.ManyToManyField('qapp.Area',related_name='area')
As schwobaseggl mentioned, that fact your models are in different apps doesn't make difference. 正如schwobaseggl所提到的那样,你的模型在不同的应用程序中的事实并没有什么不同。 Rendering fields connected via ManyToMany
relationship should be pretty much the same. 通过ManyToMany
关系连接的渲染字段应该几乎相同。
If I understood correctly, the problem was that you wasn't using a related_name
, and the code snippet you shared wasn't really your actual code. 如果我理解正确,问题是你没有使用related_name
,你共享的代码片段实际上并不是你真正的代码。
But just for the sake of completeness, in case someone find this question in the future: 但仅仅为了完整起见,以防有人在将来发现这个问题:
The use of related_name
is not mandatory. 使用related_name
不是必需的。 If the ManyToMany
relationship were defined as: 如果ManyToMany
关系定义为:
class Place(models.Model):
# ...
area = models.ManyToManyField('qapp.Area')
In a Area
instance, you would be able to navigate like this: 在Area
实例中,您可以像这样导航:
area = Area.objects.get(pk=1)
places = area.place_set.all()
Because Django automatically adds a reverse relationship %(model_name)_set
. 因为Django会自动添加反向关系%(model_name)_set
。 If you set a related_name
, it will override the default name. 如果设置了related_name
,它将覆盖默认名称。
Finally, to enhance the readability of the code, the relationship with the Area
model would be better that way: 最后,为了增强代码的可读性,与Area
模型的关系会更好:
class Place(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100, primary_key=True)
areas = models.ManyToManyField('qapp.Area', related_name='places')
Using the plural form for the ManyToMany
relationship. 使用ManyToMany
关系的复数形式。 After all, it's a collection of Area
and Place
on both sides, so areas
and places
. 毕竟,它是两侧的Area
和Place
的集合,所以areas
和places
。
Now, to render a ManyToMany
field in the template you could do it like this (considering your view returned an Area
instance named area
: 现在,要在模板中呈现ManyToMany
字段,您可以这样做(考虑到您的视图返回了一个名为area
的Area
实例:
<h1>Currect area: {{ area.name }}</h1>
<h2>Places:</h2>
<ul>
{% for place in area.places.all %}
<p>{{ place.name }}</p>
{% endfor %}
</ul>
There are 2 ways we can show m2m fields data in template. 我们有两种方法可以在模板中显示m2m字段数据。
If you want to show all the places related to an area; 如果要显示与某个区域相关的所有位置; see @Vitor's answer. 看@Vitor的答案。
If you want to show all the areas related to a single place; 如果要显示与单个地点相关的所有区域; see below - 见下文 -
queryset for the places place1 = Place.objects.filter(id=1)
places的查询集place1 = Place.objects.filter(id=1)
Now in the template : 现在在模板中 :
{% for place2 in place1 %}
{% for area1 in place2.area.all %}
<p>{{area1.name}}</p>
{% endfor %}
{% endfor %}
I have deliberately taken variables like above so that someone new can understand where to put which variable. 我故意采取上面的变量,以便新的人可以理解在哪里放哪个变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.