@@ -440,7 +440,7 @@ table using the field value will return exactly one result.
440
440
This is implemented as a ``Model.objects.get() `` query, so if the instance in not uniquely identifiable based on the
441
441
given arg, then the import process will raise either ``DoesNotExist `` or ``MultipleObjectsReturned `` errors.
442
442
443
- See also :ref: `advanced_usage:Creating non existent relations `.
443
+ See also :ref: `creating- non- existent- relations `.
444
444
445
445
Refer to the :class: `~.ForeignKeyWidget ` documentation for more detailed information.
446
446
@@ -477,26 +477,38 @@ declaration.
477
477
class Meta:
478
478
model = Book
479
479
480
- Creating non existent relations
480
+ .. _creating-non-existent-relations :
481
+
482
+ Creating non-existent relations
481
483
-------------------------------
482
484
483
485
The examples above rely on the relation data being present prior to the import. It is a common use-case to create the
484
- data if it does not already exist. It is possible to achieve this as follows::
486
+ data if it does not already exist. A simple way to achieve this is to override the ``ForeignKeyWidget ``
487
+ :meth: `~import_export.widgets.ForeignKeyWidget.clean ` method::
488
+
489
+ class AuthorForeignKeyWidget(ForeignKeyWidget):
490
+ def clean(self, value, row=None, **kwargs):
491
+ try:
492
+ val = super().clean(value)
493
+ except Author.DoesNotExist:
494
+ val = Author.objects.create(name=row['author'])
495
+ return val
496
+
497
+ Now you will need to declare the widget in the Resource::
485
498
486
499
class BookResource(resources.ModelResource):
487
500
488
- def before_import_row(self, row, **kwargs):
489
- author_name = row["author"]
490
- Author.objects.get_or_create(name=author_name, defaults={"name": author_name})
501
+ author = fields.Field(
502
+ attribute="author",
503
+ column_name="author",
504
+ widget=AuthorForeignKeyWidget(Author, "name")
505
+ )
491
506
492
507
class Meta:
493
508
model = Book
494
509
495
- The code above can be adapted to handle m2m relationships.
496
-
497
- You can also achieve similar by subclassing the widget :meth: `~import_export.widgets.ForeignKeyWidget.clean ` method to
498
- create the object if it does not already exist. An example for :class: `~import_export.widgets.ManyToManyWidget ` is
499
- `here <https://github.com/django-import-export/django-import-export/issues/318#issuecomment-861813245 >`_.
510
+ The code above can be adapted to handle m2m relationships, see
511
+ `this thread <https://github.com/django-import-export/django-import-export/issues/318#issuecomment-861813245 >`_.
500
512
501
513
Customize relation lookup
502
514
-------------------------
@@ -537,7 +549,7 @@ Then if the import was being called from another module, we would pass the ``pub
537
549
538
550
>>> resource = BookResource(publisher_id=1)
539
551
540
- If you need to pass dynamic values to the Resource from an ` Admin integration `_ , refer to
552
+ If you need to pass dynamic values to the Resource when importing via the Admin UI , refer to
541
553
See :ref: `dynamically_set_resource_values `.
542
554
543
555
Django Natural Keys
0 commit comments