I am new to django, so the question may be silly, but please feel free to teach me the right way if you know it. I tried to deal with the problem, but I'm still losing. Here is 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 opinion, I get a partial list of X objects and a partial list of Y objects, for example:
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 would like to be able to display the table as follows:
<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>
</td>
{% endfor %}
</tr>
{% endfor %}
How to do it right in django?
Many thanks,
mfynf source
share