I can’t seem to determine what a model’s field type is from within a template. I’m iterating through all rows and fields and want to implement special handling for certain field types, but it doesn’t work. Here’s how my object looks in models.py:
class MyModel(models.Model)
Field1 = models.CharField(max_length=30)
Field2 = models.DateTimeField()
And this is what is in views.py:
def MyView(request):
entries = serializers.serialize( "python", MyModel.objects.all()[:10] )
return render_to_response('MyTemplate.html', entries, context_instance=RequestContext(request))
And finally, my template file:
<table>
{% for entry in entries %}
<tr>
{% for field, value in entry.fields.items %}
<td>
{% if field|field_type = "DateTimeField" %}
hooray!, this is a DateTimeField
{% else %}
boo! no this is not a DateTimeField!
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
You’d think that I’d end up with one “hooray” and one “boo”, but I’m getting all “boo”'s
I’ve tried “field.type” and a number of other ways but nothing seems to return what I’m looking for. Can anyone offer help on this?
Sorry, not “scripting dictionary”. Anyhow…
Nobody responded, but I did find a solution, so I’ll post it here for the sake of being polite in case someone else has the same problem in the future. I decided to just use a nested dictionary instead of going with the serialized solution previously posted. So my model is the same, but here’s my new view:
def MyView(request):
MyQuerySet = MyModel.objects.all()[:10]
MyDictionary = {}
RowNumber = 0
for row in MyQuerySet:
NestedDictionary = {}
ColumnNumber = 0
for column in row._meta.fields:
if column.verbose_name != "ID":
fieldtype = str(type(column)).replace("'>", "").replace("<class 'django.db.models.fields.", "")
NestedDictionary.update({str(ColumnNumber):
{'name': column.verbose_name,
'value': getattr(row, column.verbose_name),
'type': fieldtype}
})
ColumnNumber = ColumnNumber + 1
MyDictionary.update({str(RowNumber): NestedDictionary})
RowNumber = RowNumber + 1
ctx = {}
ctx.update({'MyDictionary':MyDictionary})
return render_to_response('MyTemplate.html', ctx, context_instance=RequestContext(request))
…and here’s my template:
<table cellpadding="1px" cellspacing="1px" border="1px">
{% for key, row in MyDictionary.items %}
<tr>
{% for key2, field in row.items %}
<td>
{% if field.type == "DateTimeField" %}
hooray!, this is a DateTimeField
{% else %}
boo! no this is not a DateTimeField!
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Since someone else writes the var names with a lot of our stuff, I make pretty heavy use of stuff like
<p>{{ field | pprint }}</p>
in my pages. I do this a lot for objects/dictionaries where I’m not sure what all properties they have, or I know they have some kind of property but not its name.
Or, if I’m looking at the shop running in a terminal, in the .py file I can
print field
to see in the terminal, but prettyprint in JInja2 makes a nice filter.
This is a quick poor-man’s debugging/development tool, but it’ll give you a better idea of what your objects actually have in them, and what to call individuals inside.