ManyToMany relation with intermediate model breaks import
See original GitHub issueLet’s say I need to modify the example app to add a field on the relation table between a book and a category:
# models.py
@python_2_unicode_compatible
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
@python_2_unicode_compatible
class Book(models.Model):
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, blank=True, null=True)
author_email = models.EmailField('Author email', max_length=75, blank=True)
imported = models.BooleanField(default=False)
published = models.DateField('Published', blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, null=True,
blank=True)
categories = models.ManyToManyField(Category, blank=True,
through="IntermediateRelation")
def __str__(self):
return self.name
@python_2_unicode_compatible
class IntermediateRelation(models.Model):
relation_field = models.CharField(max_length=100)
category = models.ForeignKey(Category)
book = models.ForeignKey(Book)
def __str__(self):
return self.relation_field
Then I update admin.py
so that I can assign a category to a book in the admin:
class IntermediateRelationInline(admin.TabularInline):
model = IntermediateRelation
extra = 1
class BookAdmin(ImportExportMixin, admin.ModelAdmin):
list_filter = ['categories', 'author']
inlines = (IntermediateRelationInline, )
I can export my books, but when I try to import them back, here is what I get:
Traceback:
File "/home/synext/lib/python2.7/site-packages/Django-1.7-py2.7.egg/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/synext/lib/python2.7/site-packages/Django-1.7-py2.7.egg/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/home/synext/lib/python2.7/site-packages/Django-1.7-py2.7.egg/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/synext/lib/python2.7/site-packages/Django-1.7-py2.7.egg/django/contrib/admin/sites.py" in inner
204. return view(request, *args, **kwargs)
File "../import_export/admin.py" in process_import
130. raise_errors=True)
File "../import_export/resources.py" in import_data
355. six.reraise(*sys.exc_info())
File "../import_export/resources.py" in import_data
342. self.save_m2m(instance, row, real_dry_run)
File "../import_export/resources.py" in save_m2m
216. self.import_field(field, obj, data)
File "../import_export/resources.py" in import_field
195. field.save(obj, data)
File "../import_export/fields.py" in save
94. setattr(obj, self.attribute, self.clean(data))
File "/home/synext/lib/python2.7/site-packages/Django-1.7-py2.7.egg/django/db/models/fields/related.py" in __set__
1170. raise AttributeError("Cannot set values on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead." % (opts.app_label, opts.object_name))
Exception Type: AttributeError at /admin/core/book/process_import/
Exception Value: Cannot set values on a ManyToManyField which specifies an intermediary model. Use core.IntermediateRelation's Manager instead.
Issue Analytics
- State:
- Created 9 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Keep relationships between many-to-many fields django-tables2
The relationships between the three fields are broken. Is there a way to correct this? And the separator could be a newline and...
Read more >ManyToMany relation with intermediate model breaks import
Let's say I need to modify the example app to add a field on the relation table between a book and a category:...
Read more >Models - Django 1.4 documentation
When you set up the intermediary model, you explicitly specify foreign keys to the models that are involved in the ManyToMany relation.
Read more >Django Rest Framework - ManyToMany relationship through ...
Django Rest Framework - ManyToMany relationship through intermediate model ; from __future__ import ; import models class ; 50) def ; self): return ......
Read more >django.db.models.fields.related - Django documentation
ManyToManyField (Target) # rel_opts.object_name == "Target" rel_opts = self.remote_field.model._meta # If the field doesn't install a backward relation on ...
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
@amarandon Thank you, that was extremely helpful.
Just a note on your is_m2m_with_intermediate_object and get_intermediate_model functions: _meta.get_field_by_name has been deprecated and will be removed soon.
You can use _meta.get_field instead. Eg:
I managed to get it working. I’m posting what I’ve got so far for the record:
Some of this code is specific to the project I’m working on, for instance the
related_object_representation
method that has a couple of hardcoded fields but it should be possible to adapt.