Reputation: 2199
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...
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
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>
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.
The question is, how can I manipulate the data sending it to the template, and still follow the ForeignKey?
Upvotes: 32
Views: 36895
Reputation: 5388
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 %}
This is covered in the QuerySet.values() documentation, near the bottom:
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'}, ...]
Upvotes: 64