#26807: Custom fields do not call to_python on assignment
----------------------------------------------+--------------------
     Reporter:  akx                           |      Owner:  nobody
         Type:  Uncategorized                 |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.9
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 With the deprecation of `SubfieldBase`, custom fields no longer
 automatically have the `Creator` class behavior that calls `to_python()`
 on every assignment (see
 
https://github.com/django/django/blob/4fd264b6f1737b0317fdd95b3d7ff3bba15ae6c3/django/db/models/fields/subclassing.py#L44),
 incl. construction.

 This causes the problem described here – https://github.com/hzdg/django-
 enumfields/issues/60 – i.e. `enumfields.EnumField` doesn't coerce values
 to the actual `Enum` value types anymore.

 The fix is naturally to reimplement a descriptor like `Creator` (and that
 does indeed fix this particular issue), but I feel like I'm missing
 something doing that.

 (The deprecation warning for SubfieldBase says that one should use
 `from_db_value()`, but it doesn't get called on instance construction
 either, it seems.)

 Here's a trivial py.test style test case (the assertions fail):

 {{{
 from django.db import models

 class CoolCharField(models.CharField):
     def to_python(self, value):
         return str(value).upper() + "!!!"

     def from_db_value(self, value):
         return self.to_python(value)


 class CoolModel(models.Model):
     yay = CoolCharField()


 def test_cool_char_field_1():
     m = CoolModel(yay="ponies")
     assert m.yay == "PONIES!!!"


 def test_cool_char_field_2():
     m = CoolModel()
     m.yay = "ponies"
     assert m.yay == "PONIES!!!"
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26807>
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/046.89e783e0219e9a3b3ef40d9e66b9bd61%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to