#29346: Add "intermediary" kwarg to ModelForm._save_m2m
-----------------------------------+--------------------------------------
     Reporter:  Douglas Denhartog  |                    Owner:  nobody
         Type:  New feature        |                   Status:  new
    Component:  Forms              |                  Version:  2.0
     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
-----------------------------------+--------------------------------------

Comment (by Douglas Denhartog):

 Replying to [comment:4 Tim Graham]:
 > Sorry, what I meant was, why isn't `Meta.exclude` sufficient for this
 use case. If you added a test to your branch that could demonstrate the
 use case.
 >
 > Looking at the existing implementation of `_save_m2m()`, a possible
 alternative (that doesn't require changes to Django) could be to remove
 the field from `self.cleaned_data` after you save  the intermediate model
 manually.

 **To specifically answer your question**: `Meta.exclude` is not sufficient
 when someone wants to **include** a model intermediary many-to-many field
 in a ModelForm. If one uses `Meta.exclude`, per the Django docs, one must
 "manually add the excluded fields back to the form, [and the field] will
 not be initialized from the model instance." Further, as you mentioned,
 without my solution, one must **also** manipulate `self.cleaned_data`.

 Thus, without my solution, one must:
 (1) manually add the intermediate data to the form instance (vs. my
 solution where Django does this itself);
 (2) manually add the form field (both solutions require);
 (3) (within `ModelForm.save()`) remove the field from cleaned_data, then
 `super().save()`, then add the field back into `self.cleaned_data` (vs. my
 solution where this is unnecessary).

 I'll build some tests to demonstrate (will be a couple of days until I can
 do so).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29346#comment:5>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.ad672eadc5b6677bb7d1e08feaed34db%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to