Невозможно присвоить объект, который должен быть экземпляром

У меня есть эти Models:

class InvestorProfile(SealableModel):
    investor_type = models.CharField(max_length=200, choices=investor_type_choices)
    account = models.ForeignKey('app.Account', related_name='add_account_investorprofile', blank=False, null=False, on_delete=models.CASCADE)

class Account(SealableModel):
    contact = models.OneToOneField('app.Contact', on_delete=models.CASCADE)

class Contact(SealableModel):
    firstname = models.CharField(max_length=200)
    lastname = models.CharField(max_length=200, blank=True, null=True)
    email = models.EmailField(max_length=200)

и я хочу добавить Contact, Account и InvestorProfile соответственно при импорте InvestorProfile с помощью django-import-export.

Как я делаю это с помощью django-import-export'after_import_instance.

def after_import_instance(self, instance, new, row_number=None, **kwargs):
    """
    Create any missing Contact, Account, and Profile entries prior to importing rows.
    """
    try:
        # check if the investor type is company, ind, etc.
        # retrieve the correct object depending on the investor type
        # do the logic below
        
        if self.investorprofile__add_associated_account__contact__email:
            # create contact first
            contact, created = Contact.objects.seal().get_or_create(email=self.investorprofile__add_associated_account__contact__email)
            # add firstname and lastname
            contact.firstname = self.investorprofile__add_associated_account__contact__firstname
            contact.lastname = self.investorprofile__add_associated_account__contact__lastname
            # save
            contact.save()

            # # check if account exists
            account, created = Account.objects.seal().get_or_create(contact=contact)

            # # check if investorprofile exists
            investorprofile, created = InvestorProfile.objects.seal().get_or_create(add_associated_account=account, investor_type=self.investorprofile__investor_type)

            instance.investorprofile = investorprofile

    except Exception as e:
        print(e, file=sys.stderr)

Все выглядит нормально, пока я не столкнулся с этим в представлении сообщения об ошибке:

enter image description here

INVESTORPROFILE__ADD_ASSOCIATED_ACCOUNT
  Cannot assign "'sample@email.com'": "InvestorProfile.add_associated_account" must be a "Account" instance.

что вызывает недоумение, поскольку эта строка возвращает объект счета.

# # check if account exists
account, created = Account.objects.seal().get_or_create(contact=contact)

Есть ли что-то, что я упускаю? Любая помощь будет принята с благодарностью.

Вернуться на верх