Django import-export import foreign keys that do not exist
Being in a rush I'm having a hard time understanding the import concept.
Where is the final board creation missing? i.e. Board(person=p, organization=p)
Model
class Person(BaseModel):
organizations = models.ManyToManyField("Organization",
blank=True,
through="Board")
class Organization(BaseModel):
people = models.ManyToManyField("Person", blank=True, through="Board")
class Board(BaseModel):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
Test
from django.test import TestCase
import tablib
from import_export import resources
class BoardResource(resources.ModelResource):
def before_import_row(self, row, **kwargs):
org_name_1 = row["organization"]
o=Organization.objects.get_or_create(name_1=org_name_1, defaults={"name_1": org_name_1})
person_firstname = row["person"]
p=Person.objects.get_or_create(firstname=person_firstname, defaults={"firstname": person_firstname})
class Meta:
model = Board
dataset = tablib.Dataset(['','john','acme'], headers=['id','person','organization'])
class TestCase(TestCase):
def test_basic_import(self):
board_resource = BoardResource()
result = board_resource.import_data(dataset, dry_run=False)
print(result.totals)
assert not result.has_errors()
The documentation points to this thread though I'm unable to apply anything to my case