views:

48

answers:

2

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 data for a template? For each domain want to display table (figures in table - position):

+----------+--------+--------+-------+--------
| keyword  | date1  | date2  | date3 | ...
+----------+--------+--------+-------+--------
| keyword1 |    2   |    6   |    7  |   ...
+----------+--------+--------+-------+--------
| keyword2 |    4   |   12   |    5  |   ...
+----------+--------+--------+-------+--------
| keyword3 |    6   |    3   |    9  |   ...
+----------+--------+--------+-------+--------

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)

How to get positions?

A: 

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.

jellybean
And don't forget you don't need the brackets after `all` when you're in the template.
Daniel Roseman
do we need "all" atall?
anand
+1  A: 
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 %}
anand