Fixed #25143 -- Added ArrayField.from_db_value().

Thanks Karan Lyons for contributing to the patch.
This commit is contained in:
Fernando Miranda 2015-07-22 14:51:05 -03:00 committed by Tim Graham
parent f8d20da047
commit 2495023a4c
4 changed files with 65 additions and 1 deletions

View file

@ -6,6 +6,35 @@ from .fields import (
)
class Tag(object):
def __init__(self, tag_id):
self.tag_id = tag_id
def __eq__(self, other):
return isinstance(other, Tag) and self.tag_id == other.tag_id
class TagField(models.SmallIntegerField):
def from_db_value(self, value, expression, connection, context):
if value is None:
return value
return Tag(int(value))
def to_python(self, value):
if isinstance(value, Tag):
return value
if value is None:
return value
return Tag(int(value))
def get_prep_value(self, value):
return value.tag_id
def get_db_prep_value(self, value, connection, prepared=False):
return self.get_prep_value(value)
class PostgreSQLModel(models.Model):
class Meta:
abstract = True
@ -38,6 +67,7 @@ class OtherTypesArrayModel(PostgreSQLModel):
ips = ArrayField(models.GenericIPAddressField())
uuids = ArrayField(models.UUIDField())
decimals = ArrayField(models.DecimalField(max_digits=5, decimal_places=2))
tags = ArrayField(TagField(), blank=True, null=True)
class HStoreModel(PostgreSQLModel):