Importing a ManyToManyField with an intermediary model (using "through" argument) using the ManyToManyWidget #1335
-
I have been using the widgets in this package and have found them very helpful. But recently while trying to import a model which has a ManyToManyField that uses a custom intermediary model specified using the "through" argument in the ManyToManyField, I couldn't find anything related in the documentation. Also, the fields in this intermediary model are mandatory, so trying to import without these fields results in an error. So, it would be great if you could let me know some way to import such a model, and maybe this could be added to the official documentation too. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I was doing some thought on this and I don't think there is a way to do this because through models would need to exist already. The overall way import export works is that it does not create any models except the primary model, so relations need to already exist in the target system (including through models). I have done a lot of work for work with serializing relationships and creating them, so I can give you some feedback that it is very possible to serialize additional models. The two approaches I have explored are:
In the case of a Through model, where your target model (e.g. Group, in the User-Membership-Group paradigm that is a common example) already exists, I think a SerializerWidget would be the right way to approach this. Unfortunately that capability does not exist in Import-Export. If you are still interested in this functionality, you might find some useful info in this discovery thread on using Resources for fields There are also a few stackoverflows out there I think about ways to create a custom widget to hydrate related models. Basically you can make a widget that creates the model during the Clean method instead of just relating to. |
Beta Was this translation helpful? Give feedback.
I was doing some thought on this and I don't think there is a way to do this because through models would need to exist already. The overall way import export works is that it does not create any models except the primary model, so relations need to already exist in the target system (including through models).
I have done a lot of work for work with serializing relationships and creating them, so I can give you some feedback that it is very possible to serialize additional models.
The two approaches I have explored are: