#25143: ArrayField should implement from_db_value()
----------------------------------+--------------------------------------
     Reporter:  Odahi             |                    Owner:
         Type:  Bug               |                   Status:  new
    Component:  contrib.postgres  |                  Version:  1.8
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Changes (by Odahi):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old 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
>
> }}}
>
> 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.

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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.bb3a5cc77d5323e158eda27eee351f36%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to