Reputation: 3214
There is a model:
class DomainPosition(models.Model):
domain = models.ForeignKey(Domain)
keyword = models.ForeignKey(Keyword)
date = models.DateField()
position = models.IntegerField()
class Meta:
ordering = ['domain', 'keyword']
How to get the positions records for a template if for each domain I want to display a next table (figures in the table are position values):
+----------+--------+--------+-------+--------
| keyword | date1 | date2 | date3 | ...
+----------+--------+--------+-------+--------
| keyword1 | 2 | 6 | 7 | ...
+----------+--------+--------+-------+--------
| keyword2 | 4 | 12 | 5 | ...
+----------+--------+--------+-------+--------
| keyword3 | 6 | 3 | 9 | ...
+----------+--------+--------+-------+--------
where views.py
:
def show_domain_history(request, domain_name):
domain = Domain.objects.filter(name__contains=domain_name)
if not domain:
return HttpResponseRedirect('/')
else:
# positions = ...
variables = RequestContext(request, {
'domain': domain[0].name,
'positions': positions,
})
return render_to_response('history.html', variables)
Upvotes: 1
Views: 451
Reputation: 23427
def show_domain_history(request, domain_name):
domain = Domain.objects.filter(name__contains=domain_name)
if not domain:
return HttpResponseRedirect('/')
else:
variables = {'domain': domain }
return render_to_response('history.html', variables)
now in the template you can iterate through this as:
{% for dom in domain %}
name: {{ dom.name }}
{% for item in dom.domainposition_set %}
date: item.date
position: item.position
{% endfor %}
{% endfor %}
Upvotes: 1
Reputation: 29913
Since you have a foreign key from DomainPosition
to Domain
, you should be able to get the set of all domain positions that reference a particular domain dom
with dom.domainposition_set.all()
.
You can then build your table by iterating over this list of domain positions in your template.
Upvotes: 0