Two foreign keys and value in django template

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>
      <!-- here I need help: 
           I need to display z[x, y] if it exists, or "N/A" otherwise.
      -->
     </td>
    {% endfor %}
   </tr>
  {% endfor %}

How to do it right in django?

Many thanks,

+5
source share
4 answers

@DZPM , , . [sheepish] - "", - . , X, , Y, , Z[X, Y]. get_row get_cell, . [/]

, , , . .

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

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

, :

{% if z|x_is:x and z|y_is:y %}
    {{ z }}
{% else %}
    N/A
{% endif %}
+1

. z_list?

z_list = Z.objects.filter(x__name__contains=x_pattern, y__name__contains=y_pattern)
0

, :

# nested inside your view function
def x_and_z_list(y):
    for x in x_list:
        z_obj = x.z_set.filter(y=y)
        z_name = z_obj or 'N/A'
        yield {'x': x, 'z': z_name}
return render_to_response('mytemplate', {'list_generator': x_and_z_list}

:

{% for y in y_list %}
    <tr>
        <td>{{ y }}</td>
        {% for pair in list_generator.y %}  {# pair is the dict you yielded before #}
            <td>{{ pair.x.name }}: {{ pair.z }}</td>
        {% endfor %}
    </tr>
{% endfor %}
0

I combined the concepts of a custom filter and functions as first-class objects, turning the template filter into a functor (object-object).

Here is what I did:

def z_filter(x, y):
    z_list = list(Z.objects.filter(x, y))
    return z_list.pop().name or 'N/A'
register.filter(z_filter)

In the template:

{% load z_filter %}
<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>{{ x|z_filter:y }}</td>
    {% endfor %}
   </tr>
 {% endfor %}
</table>

Thank you all for your help!

0
source

All Articles