gpt4 book ai didi

django - m2m 到 : accessing intermediate table from templates

转载 作者:行者123 更新时间:2023-12-04 05:39:18 25 4
gpt4 key购买 nike

当 ManyToMany 关系通过直通表有额外数据时,如何获取模板中的数据?从一个角度来看,如果我提供参数,我可以获得数据:

class Category(models.Model):
title = models.CharField(max_length=1024,null=True,blank=True)
entry = models.ManyToManyField(Entry,null=True,blank=True,
related_name='category_entry',
through='CategoryEntry',
)

class CategoryEntry(models.Model):
category = models.ForeignKey(Category)
entry = models.ForeignKey(Entry)
votes = models.IntegerField(null=False, default=0)

def category_detail(request, pk):
category = models.Category.objects.select_related().get(pk=pk)
entries = category.entry.order_by('-temp_sort_order').filter(temp_sort_order__gte=0)
for entry in entries:
assert isinstance(entry, models.Entry)
ce = models.CategoryEntry.objects.get(entry=entry, category=category)
pprint('Show votes as a test: ' + ce.votes) #OK
pprint('entry title: ' + entry.title) #OK
pprint('entry votes: ' + str(entry.category_entry.votes)) #BAD
pprint('entry votes: ' + str(entry.entry.votes)) #BAD
....

但是模板不能为方法提供参数。

文档位于 https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships对模板保持沉默。使用使用 for entry in category.category_entry_set.all给出 'Category' 对象没有属性 'category_entry_set'。 category.category_entry.all也不起作用。

最终我想在模板中显示额外的数据:
{% for entry in entries %}
<ul>
<li>Title: {{ entry.title }} Votes: {{ entry.category_entry.votes }} {{ entry.entry.votes }}</li>
</ul>
{% endfor %}

最佳答案

如果模板中有一个类别实例:

category.entry.all -> list of entries

如果模板中有一个条目实例:
entry.category_entry.all -> list of categories

您应该以复数形式调用 M2M 字段,
那么你将有一个更具可读性的代码
category.entries.all

%model%_set 语法(或相关名称,如果您已指定)用于通过向后关系访问模型。

https://docs.djangoproject.com/en/1.4/topics/db/queries/#following-relationships-backward

但是如何获得与 m2m 实例相关的“投票”? – 布莱斯

我建议你通过以下方式:
class Category(models.Model):
title = models.CharField(max_length=1024,null=True,blank=True)
entries = models.ManyToManyField(Entry,null=True,blank=True,
related_name='categories',
through='CategoryEntry',
)

class CategoryEntry(models.Model):
category = models.ForeignKey(Category, related_name='category_entries')
entry = models.ForeignKey(Entry)
votes = models.IntegerField(null=False, default=0)

def category_detail(request, pk):
category = models.Category.objects.select_related().get(pk=pk)
category_entries = category.category_entries.filter(entry__temp_sort_order__gte=0).order_by('-entry__temp_sort_order')
for category_entry in category_entries:
# category_entry is an instance of the model CategoryEntry
pprint('category entry votes: ' + str(category_entry.votes))
pprint('entry title: ' + category_entry.entry.title)
....

HOW TO
entry = Entry.objects.get(pk=1)
entry.categories.all() # list of categories (here we work through related name of the field entries)

category = Category.objects.get(pk=1)
category.entries.all() # list of entries (here we work through m2m field entries)

category.category_entries.all() # list of CategoryEntry objects (through related name category_entries of the field category in model CategoryEntry)

关于django - m2m 到 : accessing intermediate table from templates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11477497/

25 4 0