How To Change An M2m Field In A Updateview?
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?"