Can't save model with ManyToMany Field
See original GitHub issueI’ve found a bug in save of m2m related objects. Steps to reproduce:
- Install wagtail CMS
- Create single Page model
- Create standard Django model
- Add
ManyToManyField
from your Page model to standard django model - Try to create page instance
When you try to save it, you’ll receive following error:
Traceback:
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/home/artur/Projekty/demo/src/wagtail/wagtail/wagtailadmin/views/pages.py" in create
172. page = form.save(commit=False) # don't save yet, as we need treebeard to assign tree params
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/modelcluster/forms.py" in save
257. self.save_m2m()
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/django/forms/models.py" in save_m2m
96. f.save_form_data(instance, cleaned_data[f.name])
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in save_form_data
1527. setattr(instance, self.attname, data)
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in __set__
835. manager = self.__get__(instance)
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in __get__
825. through=self.field.rel.through,
File "/home/artur/Projekty/demo/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in __init__
522. (instance, source_field_name))
It tries to execute save_m2m()
before saving page instance. Is there workaround for this issue?
Issue Analytics
- State:
- Created 9 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Django Model With ManyToMany Not Saving - Stack Overflow
First create and save your ManyToMany objects and then add them AFTER they have already been saved ingredient = Ingredient(add parameters ...
Read more >Many-to-many field, save() method and the Django admin ...
When you save a model via admin forms it's not an atomic transaction. The main object gets saved first (to make sure it...
Read more >Adding an object to a ManyToMany field doesn't work
Post model has a manytomany field to User model. I try to add a user object to the field with the view function...
Read more >TypeError : Unable to save m2m relation modelform
If your model has a many-to-many relation and you specify commit=False when you save a form, Django cannot immediately save the form data...
Read more >Django many-to-many relationships does't save to DB via a ...
Django, ManyToManyField save data via forms.ModelForm with commit=False and djnago many-to-many relationship does not save to db.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Finally completed in #62!
I wrote a quick guide and explanation on how to implement M2M’s using what’s currently available. https://gist.github.com/alexgleason/09b1802b004babd74e96