Import foreign keys that do not exist (creating new values in Foreign key)
See original GitHub issueI am trying to import values which do not already exist my foreign key. I would like import/export to add these new values to the ForeignKey model.
I’ve tried adding “null/blank” to the models, but it still will not create (import) new values in the foreign key. The error I get is “DoesNotExist: x matching query does not exist.”
Here is a sample of my Admin.py (which only imports to a foreign key if the values being imported already exist).
store_name = fields.Field(column_name='store_name', attribute='Store', widget=widgets.ForeignKeyWidget(Store, 'store_name'))
def clean(self, value):
val = super(ForeignKeyWidget, self).clean(value)
object, created = Store.objects.get_or_create(store_name='')
class Meta:
model = Product
fields = ('id', 'second_name', 'product_name', store',)
export_order = ('id', 'second_name', 'product_name')
skip_unchanged = False
report_skipped = False
widgets = {
'published': {'format': '%d.%m.%Y'},
}
Can anyone advise please?
Thanks
Issue Analytics
- State:
- Created 8 years ago
- Comments:17 (6 by maintainers)
Top Results From Across the Web
Foreign Key Constraint | CockroachDB Docs
The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references.
Read more >SQL insert foreign key if not exists - Stack Overflow
In MySQL, you can do this with just two statements: insert into groups (group_name) select s.group_name from (select :group_name as ...
Read more >SQL Foreign key - SQLShack
In this article let us review different ways to create a SQL foreign key, rules on updates and deletes, enabling foreign key constraints, ......
Read more >13.1.17.5 FOREIGN KEY Constraints - MySQL :: Developer Zone
Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later if...
Read more >An Essential Guide to MySQL Foreign Key By Practical ...
Once a foreign key constraint is in place, the foreign key columns from the ... a new row into the products table with...
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
I’ve written it this way:
Usage example
def clean(self, value, row=None, *args, **kwargs):
https://github.com/django-import-export/django-import-export/commit/8e81b409aae501d1f882f4a4fd458112c2eccac1#diff-948ac927a7e0c8c8d0bb8026e9a49d62L288