#12337: save_m2m() does not honor save_instance's exclude argument --------------------------------+------------------------------------ Reporter: margieroginski | Owner: melinath Type: Bug | Status: new Component: Forms | Version: SVN Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 --------------------------------+------------------------------------ Changes (by melinath):
* owner: nobody => melinath * status: reopened => new * component: Database layer (models, ORM) => Forms Comment: Just ran across this myself. My use case looks like this: {{{ class MyOtherModel(Model): pass class MyThroughModel(Model): other = models.ForeignKey(MyOtherModel) model = models.ForeignKey('MyModel') # Plus some metadata fields that are handled automatically. class MyModel(Model): m2m = ManyToManyField(MyOtherModel, through=MyThroughModel) class MyForm(ModelForm): m2m = ModelMultipleChoiceField(queryset=MyOtherModel.objects.all()) class Meta: model = MyModel exclude = ('m2m',) def save(self, commit=True): # ... special behavior to handle the field. }}} With this setup, I am trying to exclude the default generated field and its behavior. In this example, the {{{m2m}}} field name is used in both the model definition and the form definition to represent a queryset of {{{MyOtherModel}}} instances. However, I could theoretically exclude the field and put a !CharField in its place. The current save_m2m code would then blindly try to assign the value of that !CharField to the m2m field on the model. This is because rather than explicitly checking whether the field is being excluded, the code assumes that if the field's name is in the cleaned_data, it was not excluded. This is not consistent with how fields and exclude are handled elsewhere in the codebase (for example in construct_instance). As far as I can tell, this would have fit well in [https://github.com/django/django/commit/64ea5af1c992232f944ef403935d9cce316498bf 64ea5af/r8756], which added exclude support to the then-save_instance function. My guess is that it was left out as an oversight rather than intentionally. -- Ticket URL: <https://code.djangoproject.com/ticket/12337#comment:9> 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 post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.