#23549: Model form's model_to_dict using value_from_object instead of to_python
----------------------------+------------------------------------
     Reporter:  mwaterfall  |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Forms       |    Version:  1.7
     Severity:  Normal      |   Keywords:  form, modelform, field
 Triage Stage:  Unreviewed  |  Has patch:  0
Easy pickings:  0           |      UI/UX:  0
----------------------------+------------------------------------
 Background: I'm using a [https://github.com/bradjasper/django-jsonfield/
 JSONField] model field to store a list of strings in a field, and I'm
 trying to create a model form with a series of checkboxes that allows me
 to multiple select zero or more of these string options using
 `TypedMultipleChoiceField` and `CheckboxSelectMultiple`.

 Issue: When the form is rendered, the checkboxes that should be checked
 aren't checked. When saving the form, the checked values are correctly
 saved and serialised by the field.

 Cause:

 The reason the checkboxes aren't getting checked is down to the function
 `model_to_dict`, that converts a model instance to a dict ready to
 populate a ModelForm. The value is retrieved from instance fields using
 `value_from_object`. Why is `to_python` not used?

 The JSONField is following the guidelines to create a custom model field,
 overriding the `to_python` to deserialise the raw value from the DB and
 return a python object. This `model_to_dict` is bypassing this, not
 allowing custom fields to deserialise values before they are used to
 populate a form.

 Hence the form is receiving the JSON string as opposed to the deserialised
 list, and causing things to be rendered incorrectly.

 Here's the line in question:
 https://github.com/django/django/blob/1.7/django/forms/models.py#L151

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

Reply via email to