Opened 9 years ago

Closed 9 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)

arrayfield_condtionally_add_from_db_value.diff (878 bytes ) - added by Karan Lyons 9 years ago.
Patch: Conditionally add from_db_value() to ArrayField instances.

Download all attachments as: .zip

Change History (5)

by Karan Lyons, 9 years ago

Patch: Conditionally add from_db_value() to ArrayField instances.

comment:1 by Karan Lyons, 9 years ago

Type: New featureBug

comment:2 by Simon Charette, 9 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted
Version: 1.9master

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 Simon Charette, 9 years ago

Version: master1.9

comment:4 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #25143

Note: See TracTickets for help on using tickets.
Back to Top