Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two foreign keys and a value in django template

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,

like image 716
mfynf Avatar asked Nov 15 '22 07:11

mfynf


1 Answers

As @DZPM suggested, you should think of keeping the logic in your view. [sheepish] I once invented my own "Table" data structure to do something very similar. The table was had rows corresponding to X, columns corresponding to Y and cells corresponding to Z[X, Y]. I then wrote get_row and get_cell filters to do the trick in the template. [/sheepish]

That said, what you want can be accomplished using a pair of custom filters. This solution is rather verbose.

@register.filter
def x_is(value, x):
    return value.x == x

@register.filter
def y_is(value, y):
    return value.y == y

You can use these filters in the template as shown below:

{% if z|x_is:x and z|y_is:y %}
    {{ z }}
{% else %}
    N/A
{% endif %}
like image 56
Manoj Govindan Avatar answered Nov 18 '22 08:11

Manoj Govindan