{{{
class ContrivedStringedIntegerField(models.IntegerField):
def from_db_value(self, value, expression, connection, context):
return unicode(value)
}}}
Used alone in a model this will return a string, but used as part of an
ArrayField we’ll get integers instead.
from_db_value() is a no-op for performance reasons, so rather than incur
the penalty inside of ArrayField when it’s unnecessary, I propose the
attached patch, which adds a from_db_value() method to the instantiated
ArrayField if its base field has one as well.
--
Ticket URL: <https://code.djangoproject.com/ticket/25953>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "arrayfield_condtionally_add_from_db_value.diff" added.
Patch: Conditionally add from_db_value() to ArrayField instances.
* needs_better_patch: => 0
* type: New feature => Bug
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/25953#comment:1>
* version: 1.9 => master
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted
Comment:
Makes sense to me but this will need additional tests to get merged.
If you could submit your patch as Github PR it would also give it more
visibility and help reviewers run CI against it.
Concerning the implementation I think you should avoid creating a local
function to attach it as a method but declare a `_from_db_value` method
that you assign to `from_db_value` instead.
--
Ticket URL: <https://code.djangoproject.com/ticket/25953#comment:2>
* version: master => 1.9
--
Ticket URL: <https://code.djangoproject.com/ticket/25953#comment:3>
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #25143
--
Ticket URL: <https://code.djangoproject.com/ticket/25953#comment:4>