Opened 10 years ago
Closed 10 years ago
#25953 closed Bug (duplicate)
Add from_db_value() support to django.contrib.postgres.fields.ArrayField.
| Reported by: | Karan Lyons | Owned by: | |
|---|---|---|---|
| Component: | contrib.postgres | Version: | 1.9 |
| Severity: | Normal | Keywords: | ArrayField, from_db_value |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | yes | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Currently ArrayField assumes that the base field will not need to alter the returned python value from the postgres backend, as this is the case for all core fields. As an example, take this absurd field:
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.
Attachments (1)
Change History (5)
by , 10 years ago
| Attachment: | arrayfield_condtionally_add_from_db_value.diff added |
|---|
comment:1 by , 10 years ago
| Type: | New feature → Bug |
|---|
comment:2 by , 10 years ago
| Needs tests: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Version: | 1.9 → master |
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.
comment:3 by , 10 years ago
| Version: | master → 1.9 |
|---|
Patch: Conditionally add from_db_value() to ArrayField instances.