I'd probably be a bit more cautious, since get_all_field_names gets foreign keys and all sorts and the field might not be in the dictionary.
I'd suggest: # Make a frozenset of the fields for fast access: allowed_fields = frozenset(obj._meta.get_all_field_names()) for field, value in dictionary_of_field_values.iteritems(): if field in allowed_fields: setattr(obj, field, value) Euan On Jun 30, 1:36 pm, Tim Chase <django.us...@tim.thechases.com> wrote: > On 06/30/2010 02:10 AM, euan.godd...@googlemail.com wrote: > > > I think you need to be careful messing with __dict__ as Django turns > > most fields in descriptors behind the scenes so setting them in the > > __dict__ could break these. > > Yeah, that was somewhat my assumption (and thus my caveat). > > >> Well, you could do something like > > >> for name, value in dictionary_of_field_values.items(): > >> setattr(obj, name, value) > > > I'd stick to setattr and maybe verify that the key in the dictionary > > is one of the model's fields. I think there is a method on _meta > > called get_all_field_names. I've used this before to validate such > > actions. > > If that's the case, you can tweak the above to something like > > for name in obj._meta.get_all_field_names(): > if name in dict_of_field_values: > setattr(obj, name, dict_of_field_values[name]) > > -tkc -- 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.