Monday, November 21, 2011

Re: Field.to_python not working as expected for me...

Never mind, I found my problem. I was missing __metaclass__ =
SubfieldBase in the declaration for the custom field type, as
mentioned in the documentation.

"Remember: If your custom field needs the to_python() method to be
called when it is created, you should be using The SubfieldBase
metaclass mentioned earlier. Otherwise to_python() won't be called
automatically."


On Nov 22, 10:23 am, Nathan Hoad <nat...@getoffmalawn.com> wrote:
> I've written a custom field (I'm using Django nonrel) for storing
> users in a SetField. The SetField doesn't seem to handle serialising
> the set of User objects properly, so what I've done is implement the
> pre_save method, which returns a set of strings which are the ids in
> MongoDB.
>
> The documentation states that pre_save is for converting complex
> Python types to database types, which is working great. The problem
> is, the documentation states this for Field.to_python; "Converts a
> value as returned by your database (or a serializer) to a Python
> object."
>
> I'd expect that the database is returning a set of strings (which are
> the ids) but it's actually returning a list of User objects. From
> this, I would then expect that accessing the field on a model would
> return a set of User objects, but it's returning a set of string
> objects.
>
> So to_python is receiving and returning a set of User objects, but
> accessing the model's users attribute returns the raw database value.
> How can I get my custom field to return the set of User objects
> properly?

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate