I am a newbie to django, so the question might be dumb, but please feel free to teach me the right way if you know it. I tried googling the issue, but I am still at loss. Here's my problem:
I have a class in my model that has two foreign keys:
class X(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return name
class Y(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return name
class Z(models.Model):
name = models.CharField(max_length=30)
x = models.ForeignKey(X)
y = models.ForeignKey(Y)
def __unicode__(self):
return name
In my view I get a partial list of X objects and a partial list of Y objects like so:
def MyView(x_pattern, y_pattern):
x_list = X.objects.filter(name__contains=x_pattern)
y_list = Y.objects.filter(name__contains=y_pattern)
z_list = Z.objects.all()
return render_to_response({'x_list': x_list, 'y_list': y_list, 'z_list': z_list})
In my template I'd like to be able to display a table like so:
<table>
<tr>
<td>Y</td>
{% for x in x_list %}
<td>{{ x }}</td>
{% endfor %}
</tr>
{% for y in y_list %}
<tr>
<td>{{ y }}</td>
{% for x in x_list %}
<td>
<!-- here I need help:
I need to display z[x, y] if it exists, or "N/A" otherwise.
-->
</td>
{% endfor %}
</tr>
{% endfor %}
How do I do this properly in django?
Thank you very much,