Hi all,
Many apps provide new related managers to extend your django models with. For example, django-tagulous provides a TagField which abstracts an M2M relation with the Tag model, django-gm2m provides a GM2MField which abstracts an relation, django-taggit provides a TaggableManager which abstracts a relation too, django-generic-m2m provides RelatedObjectsDescriptor which abstracts a relation again.
While that works pretty well, it gets a bit complicated when it comes to encapsulating the business logic for saving such data in a form object. This is two-part problem:
- getting initial data,
- saving relations.
Currently in Django, getting initial data is done in model_to_dict, which call's the model field's value_from_object() method, bypassing form fields completely, I suggest that we add ability to do that in the Form field too.
As for saving relations, this is currently done in ModelForm.save_m2m(), which calls the model field's save_form_data() directly, for each field that's many to many or virtual, completely ignoring the form field.
This means that if one wants to customize how a form field works in terms of getting the initial data for its widget and saving relations, then they should override the model field, instead of just the form field. What could go wrong with that ? :)
Do you think it would be OK to decouple that a bit and allow the form field to have value_from_object() and save_form_data() ?
Best
James