#3982: Allow Field subclasses to coerce arbitrary Python types
-----------------------------------------------------+----------------------
Reporter: Marty Alchin <[EMAIL PROTECTED]> | Owner: adrian
Status: new | Component:
Database wrapper
Version: SVN | Resolution:
Keywords: coerce coercion | Stage:
Unreviewed
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
-----------------------------------------------------+----------------------
Changes (by Marty Alchin <[EMAIL PROTECTED]>):
* keywords: to_python => coerce coercion
* needs_better_patch: => 0
* summary: [patch] Use fields' to_python to coerce Python data types =>
Allow Field subclasses to coerce arbitrary
Python types
* needs_tests: => 0
* needs_docs: => 0
Comment:
The attached patch allows Field subclasses to define a `coerce` method
that takes a single value and returns any Python type, that will then be
attached to the model instance as the appropriate attribute. This happens
after retrieval from the database, manual instantiation of objects, and
deserialization.
The one pitfall is that, due to the way bits of Django work, the value
sent to `coerce` might be:
* a string (if the model came from the DB)
* a native Python type (if it came from a serializer, having already gone
through `to_python`)
* possibly some other situation I haven't run into yet
So the `coerce` method should take these different situations into
account.
Here's an example, from !DurationField, which will be added to #2443 soon.
{{{
#!python
def coerce(self, value):
return datetime.timedelta(seconds=float(value))
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/3982#comment:1>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---