Reputation: 522
I have a "rawqueryset" object that its fields can be vary in term of some business rules. How can I access to its count and name of fields in corresponding template?
View.py
objs=MyModel.objects.raw(sql)
return list(objs)
template.py
{%for obj in objs%}
{{obj.?}}
{{obj.?}}
.
?
{%endfor%}
Upvotes: 0
Views: 967
Reputation: 522
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]
Upvotes: 1
Reputation: 9076
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?
Upvotes: 0