Hi,

As per usual, I am probably just being really dappy, but I can't see a
good way to do this.

I have a model that has about 10 fields, and I may be populating each
record with at least one field, but probably less that 4 (could be any
4 of the 10 though). The others will be null or empty strings. Some of
the fields are foreign keys, and some are text.

Anyway, I wish the unicode method of the model to return:
"""
field_a = value_of_a,
field_c = some_other_value
"""
where field_a, and field_c are the only not null fields (or fields
where the strings are not empty) for the record (if that makes sense).

The only way I can figure out how to do it (apart from lots of
"if,elif"s, hard coding the fields) is:

non_null = [(attr, getattr(self, attr)) for attr in vars(self) if
attr != 'id' and attr[0] != '_' and getattr(self, attr) and getattr
(self, attr) != '']

This returns the data I want but it is returning the raw values from
the database which is a bit nasty, and I would have to do a lot of
post processing to make both the field name and the value pretty.

Is there a nicer way to loop through the model attributes (rather than
db fields) that would give the objects for the foreign keys instead of
the ids?

Em


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to