{{{
class Tag(object):
def __init__(self, id):
self.id = id
def __unicode__(self):
return U"Tag(%d)" % self.id
class TagField(models.SmallIntegerField):
# Dummy wrapper over SmallIntegerField.
def from_db_value(self, value, expression, connection, context):
if value is None:
return value
return Tag(int(value))
def get_prep_value(self, value):
return value
...
# models.py
class Recommendation(models.Model):
tags = ArrayField(TagField(), size=3)
def __unicode__(self):
return self.tags
}}}
Then
{{{
>>> Recommendation.objects.create(tags=[Tag(1), Tag(2)])
>>> Recommendation.objects.all()
[<Recommendation: [1, 2]>] # WRONG
}}}
But this is wrong because I got Integers instead of Tag objects:
{{{
[<Recommendation: [Tag(1), Tag(2)]>] # OK
}}}
Looking at the {{{ArrayField}}} source code seems that {{{to_python()}}}
is implemented but never called. I think {{{from_db_value()}}} should be
also implemented.
--
Ticket URL: <https://code.djangoproject.com/ticket/25143>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> Then
>
New description:
I'm using a custom field with the new Postgres {{{ArrayField}}}:
{{{
class Tag(object):
def __init__(self, id):
self.id = id
def __unicode__(self):
return U"Tag(%d)" % self.id
class TagField(models.SmallIntegerField):
# Dummy wrapper over SmallIntegerField.
def from_db_value(self, value, expression, connection, context):
if value is None:
return value
return Tag(int(value))
def get_prep_value(self, value):
return value
...
# models.py
class Recommendation(models.Model):
tags = ArrayField(TagField(), size=3)
def __unicode__(self):
return self.tags
}}}
Then
{{{
>>> Recommendation.objects.create(tags=[Tag(1), Tag(2)])
>>> Recommendation.objects.all()
[<Recommendation: [1, 2]>] # WRONG
}}}
This is wrong because I got Integers instead of Tag objects:
{{{
[<Recommendation: [Tag(1), Tag(2)]>] # OK
}}}
Looking at the {{{ArrayField}}} source code seems that {{{to_python()}}}
is implemented but never called. I think {{{from_db_value()}}} should be
also implemented.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:1>
Comment (by timgraham):
Can you propose a patch and regression test?
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:2>
Comment (by Odahi):
Replying to [comment:2 timgraham]:
> Can you propose a patch and regression test?
Hello Tim, never did it before, but I think I can submit a tentative patch
& test for this in the next days. I'll try.
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:3>
* has_patch: 0 => 1
Comment:
I've just added a tentative patch.
https://github.com/Odahi/django/tree/ticket_25143
Would be great if someone can check it.
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:4>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:5>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:6>
Comment (by timgraham):
See #25579 for a related issue (querying on complex types) that could be
fixed along with this.
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:7>
* needs_better_patch: 1 => 0
Comment:
[https://github.com/django/django/pull/6131 Updated PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:8>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:9>
* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:10>
* status: new => closed
* owner: => Tim Graham <timograham@…>
* resolution: => fixed
Comment:
In [changeset:"2495023a4cae28f494d0a6172abfac3a47a0b816" 2495023]:
{{{
#!CommitTicketReference repository=""
revision="2495023a4cae28f494d0a6172abfac3a47a0b816"
Fixed #25143 -- Added ArrayField.from_db_value().
Thanks Karan Lyons for contributing to the patch.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25143#comment:11>