On 2 sep, 05:24, "Vance Dubberly" <[EMAIL PROTECTED]> wrote:
> Wow. I guess it wasn't such a stupid question.
>
> foo._meta.fields  .. each field will give me att names but not values.

Getting an attribute by name is dead simple in Python - use getattr().

> All the methods on the fields that look like they return values only
> return the values you give them, WTF?

What about foo._meta.fields[0].get_value_from_object(foo) ?


More seriously, it took me 2 minutes playing with a model object in
the interactive shell to come up with what seems a working solution:

from django.db import models

def to_dict(self):
    return dict((f.name, f.value_from_object(self)) for f in
self._meta.fields)

models.Model.to_dict = to_dict


And now all your model object have a to_dict method that will even
provide defined default values when necessary (NB : if anyone know
about any drawback or potential problem with this solution, please let
us know).



--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to