Skip to content Skip to sidebar Skip to footer

How To Change An M2m Field In A Updateview?

SOLVED by Peter DeGlopper: Thank you for your help it solved my issues, I really do appreciate it. I was banging my head against the table. I didn't have to change my ModelForm.

Solution 1:

You're overthinking it. Handling this kind of relationship can be a little bit complicated if you need to track information on the relationship model itself (like a modified timestamp for when a particular subnet/tag pair was created) but for the model relationships you've shown here, form.save_m2m() is sufficient - it handles the m2m relationship for you.

You wouldn't even need that if you didn't need to use commit=False on your initial form save so you can set your modified_by field.

For prepopulation - mostly this looks to me like it should follow the normal behavior and prepopulate. I would probably just use the widget class rather than explicitly instantiating it (widget=forms.CheckboxSelectMultiple rather than widget=forms.CheckboxSelectMultiple()) but I don't see why that would affect it.

For both problems, you might have good results by starting with a simple ModelForm with no customizations on subnet, just exclude set. Once that's working, put in the special view code to handle modified_by. Once that's working, change to a custom widget declaration for subnet - maybe initially using the meta widgets override dictionary rather than a custom field declaration for the first pass.

Post a Comment for "How To Change An M2m Field In A Updateview?"