ContactDetail.dialing_code and ContactDetail.country. These use the
standard tuples ('AU', 'Australia') and so on.
I know I can use get_country_display() and get_dialing_code_display()
to show the longer values, but is there a more generic version where
the field name is replaced with a variable?
For example, I want to create a customised .csv file from the model
data. I can loop through the fields from my model and get the shorter
attribute values without any problems. However I'd like to do
something like this:
obj = queryset[my_item] # an instance of ContactDetail()
model = queryset.model # ContactDetail() itself
for field in model._meta.fields:
val = getattr(obj, field.name) # gets the shorter version
if field.choices: # if field is ChoiceField, convert data
to display mode
val = obj.get_field_display()
row.append(val)
Obviously the 'val = obj.get_field_display()' line doesn't work!
Is there a generic way of doing this (something like
obj.get_display(fieldname) ?) or am I going to have to hard code
separate checks for get_country_display() and
get_dialing_code_display()?
Ideally I would like to use this particular function for other models
and keep the fields as generic as possible.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment