[Answer]-How to get field's names of rawqueryset in Django

1👍

I solved this issue using two filters:

template.py:

{% load my_tags %}

{%for obj in objs%}
    {%for key in obj|get_dict %}
        {% with d=obj|get_dict  %}
            {{key}} - {{ d|get_val:key }}
        {% endwith %}
    {%endfor%}
{%endfor%}

my_tags.py:

@register.filter(name='get_dict')
def get_dict(v):
    return v.__dict__   

@register.filter(name='get_val')
def get_val(value, arg):
    return value[arg]
👤mrbf

0👍

Must be honest, I’m not sure about the properties of a RawQuerySet. If it was a normal QuerySet, I’d try this.

{% for obj in objs.values %}
    {% for key, val in values.items %}
        {{ key }}: {{ val }}
    {% endfor %}
{% endfor %}

Does that do the trick?

👤meshy

Leave a comment