Django-импорт-экспорт update_or_create с UniqueConstraint

У меня есть этот Profile model вместе с constraint похожим на unique_together:

class Profile(models.Model):
    #Personal Information
    firstname = models.CharField(max_length=200)
    lastname = models.CharField(max_length=200, blank=True, null=True)
    email = models.EmailField(max_length=200)
    investor_type = models.CharField(max_length=200, choices=investor_type_choices)

    class Meta:
    constraints = [
        models.UniqueConstraint(fields=['email', 'investor_type'], name='email and investor_type')
    ]

Я хочу реализовать функцию update_or_create на Profile, которая использует email и investor_type в качестве аргумента для поиска object.

Я попробовал добавить это в свой ProfileResource:

def before_import_row(self, row, row_number=None, **kwargs):
    try:
        self.email = row["email"]
    except Exception as e:
        self.email = None

    try:
       self.investor_type = row["investor_type"] 
    except Exception as e:
        self.investor_type = None

def after_import_instance(self, instance, new, row_number=None, **kwargs):
    try:
        # print(self.isEmailValid(self.email), file=sys.stderr)
        if self.email and self.investor_type:
            profile, created = Profile.objects.update_or_create(
                email=self.email, 
                investor_type=self.investor_type,
                defaults={
                    'firstname': 'helloo',
                    'lastname': 'wooorld',
                })

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

но добавление несуществующего Profile object:

enter image description here

enter image description here

через django-import-export:

enter image description here

уже выдает ошибку, Profile with this Email and Investor type already exists несмотря на то, что ее вообще не существует.

enter image description here

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