Django:使用objects.values()并在模板中获取ForeignKey数据 [英] Django: using objects.values() and get ForeignKey data in template
问题描述
我有一个Django应用,我的主模型具有指向其他数据库表的ForeignKey字段。
I have a Django app where my main Model has ForeignKey fields to other DB tables.
class Bugs(models.Model):
bug_id = models.PositiveIntegerField(primary_key=True)
bug_severity = models.ForeignKey(Bug_severity,null=True)
priority = models.ForeignKey(Priority,null=True)
bug_status = models.ForeignKey(Bug_Status,null=True)
resolution = models.ForeignKey(Resolution,null=True)
etc...
所有ForeignKey表都有一个unicode函数,该函数返回要在模板中显示的名称。
All of the ForeignKey tables have a unicode function that returns the name that I want displayed in the template.
class Priority(models.Model):
value = models.CharField(max_length=64)
sortkey = models.PositiveSmallIntegerField()
isactive = models.NullBooleanField()
visibility_value_id = models.SmallIntegerField(null=True,blank=True)
def __unicode__(self):
return self.value
在视图中,我将查询运行为:
In the view, I am running the query as:
bugs = Bugs.objects.filter(active=True).order_by('priority__sortkey','bug_severity__sortke
在模板中,我可以遍历它们,并显示ForeignKey
In the template, I can iterate through them, and display the ForeignKey value correctly.
{% for bug in bugs %}
<tr class="bugrow" >
<td>{{bug.bug_id}}</td>
<td>{{bug.priority}}</td>
<td>{{bug.bug_severity}}</td>
<td>{{bug.bug_status}}</td>
<td>{{bug.resolution}}</td>
我遇到的问题是,在将Bug数据发送到模板之前,我需要对其进行操作,所以我使用values()方法返回字典。当我将该字典传递给模板时,它不会显示任何指向ForeignKey的字段。
The problem I am having is that I need to manipulate the Bugs data before sending it to the template, so I use the values() method to return a dictionary. When I pass that dictionary to the template it does not show any fields that point to a ForeignKey.
我很确定原因是这些值只返回了
I'm pretty sure that the reason is that the values only returns the actual database value, so it cannot follow the FK.
问题是,如何操纵将其发送到模板的数据并仍然遵循ForeignKey?
The question is, how can I manipulate the data sending it to the template, and still follow the ForeignKey?
推荐答案
我一直都在使用这种方法。你是对的。它仅返回值,因此您必须使用 __表示法从ForeignKey获取值。例如:
I use this method all of the time. You are correct. It only returns the values, so you have to use the "__" notation to get the value from the ForeignKey. For example:
# Get all of the bugs
bugs = Bugs.objects.filter(
active=True,
).order_by(
'priority__sortkey',
'bug_severity__sortkey',
).values(
'bug_id',
'priority', # Only the pk (id) of the bug priority FK
'priority__value', # Equal to bug.priority.value
'bug_severity',
'bug_severity__some_value', # Equal to bug.priority.some_value
)
现在,在您的模板中,您将执行以下操作:
Now, in your template, you do:
{% for bug in bugs %}
<tr class="bugrow">
<td>{{ bug.bug_id }}</td>
<td>{{ bug.priority }}</td>
<td>{{ bug.priority__value }}</td>
<td>{{ bug.bug_severity }}</td>
<td>{{ bug.bug_severity__some_value }}</td>
</tr>
{% endfor %}
QuerySet.values()文档,靠近底部:
您还可以通过OneToOneField,ForeignKey和ManyToManyField属性引用具有反向关系
的相关模型上的字段:
You can also refer to fields on related models with reverse relations through OneToOneField, ForeignKey and ManyToManyField attributes:
Blog.objects.values('name', 'entry__headline')
[{'name': 'My blog', 'entry__headline': 'An entry'},
{'name': 'My blog', 'entry__headline': 'Another entry'}, ...]
这篇关于Django:使用objects.values()并在模板中获取ForeignKey数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!