0

Looping over a list of records, Django displays the literal value of each record. But, what if the field is a foreign key? So, here is an example

{% for record in records %}
    {{ record.get_fields|dict_key:field }}
{% endfor %}

The dict_key is a template filter that imitates a dictionary get function given the field.

@register.filter(name = 'dict_key')
def dict_key(a_dict, key):

    the_key = normalize(key) # lowercases the key

    return the_dict.get(the_key, None)

So, if a field is a foreign key in the model, Django will display the foreign key ID. Say, for example, the field is room, Django will show '1' (the foreign key ID for room) for the first record, '2' for the second record, etc. But, I want to know how to generically and programmatically render the value of the foreign key, e.g. 'Kitchen' for the foreign key with ID 1, 'living room' for the foreign key with ID 2, etc.

Any help is appreciated

Edit 1:
The idea is to create a generic template that renders the records based on the type of fields (e.g. currency field to use custom currency filter) in a table. For that to work, I have to loop over the fields

{% for record in records %}
<tr>
    {% for field in relevant_fields%}
        <td>
        {% if field in currency_fields %}
            {{ record.get_fields|dict_key:field|currency }}
        {% else %} 
            {% record.get_fields|dict_key:field}}
        {% endif %}
        </td>
    {% endfor %}
{% endfor %}

So, I'm looking up the field name using the get_fields helper method of the model and based on the type of the filed, I am formatting my output. This way, I get to reuse the same table template.

EarlyCoder
  • 1,213
  • 2
  • 18
  • 42
  • Use `getattr(a_dict, key)` instead. – Willem Van Onsem Jul 29 '18 at 16:51
  • Apparently, getattr does not work on dictionaries. See the post: https://stackoverflow.com/q/3089186/3549766. Can you please elaborate how you would use getattr in the context of dictionaries? I got this exception: AttributeError – EarlyCoder Jul 29 '18 at 17:03
  • no, that's correct, but normally a `Record` is a model instance... – Willem Van Onsem Jul 29 '18 at 17:09
  • You are right! It is generally a model instance, but I am using a dictionary in this case with {'field_name': 'field_value', } format. – EarlyCoder Jul 29 '18 at 17:52
  • Why are you using a dictionary? – wdfc Jul 29 '18 at 18:52
  • You should show what `records` is and how it is defined. Normally, you would pass model instances, not dictionaries, to the template; if you did so then you could follow the foreign keys automatically. *Why* are you passing dictionaries? – Daniel Roseman Jul 29 '18 at 21:09
  • Daniel Roseman and wdfc, please see the Edit 1 – EarlyCoder Jul 30 '18 at 15:49
  • hey @EarlyCoder If you are trying to add generic fields, you should look into djangos generic relationships (here is a really good blog article https://simpleisbetterthancomplex.com/tutorial/2016/10/13/how-to-use-generic-relations.html) or another option is using JSON fields https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/#jsonfield. As for your problem with actually getting the dict field, does this help you at all? https://stackoverflow.com/questions/43492271/jsonfield-django-template-doesnt-show-me-what-i-went Sorry I am a bit lazy to try myself :P – wdfc Jul 31 '18 at 00:10

0 Answers0