【问题标题】:django-import-export update profile if email exists如果电子邮件存在,django-import-export 更新配置文件
【发布时间】:2022-01-04 02:38:02
【问题描述】:

我的Profile model 上有这个unique field:

email = models.EmailField(max_length=200, unique=True)

我有这个ProfileResource:

class ProfileResource(resources.ModelResource):

    # associated_issuer = fields.Field(attribute='add_associated_issuer', widget=ManyToManyWidget(Issuer, field='name'), column_name='Associated Issuer')
    email = fields.Field(attribute='email', 
        widget=CharWidget(), 
        column_name='email')

    class Meta:
        model = Profile
        clean_model_instances = True
        import_id_fields = ('id', 'email')

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

        try:
            if row["id"] == '':
                row["id"] = None
                self.id = row["id"] 
        except Exception as e:
            self.id = None

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

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

现在当我尝试从文件导入时,我收到此错误:

我需要能够执行update_or_create 方法,但我在哪里添加该代码?我尝试在after_import_instance 上执行此操作,但没有成功。

我也尝试过import_row 这样的:

def import_row(self, row, instance_loader, using_transactions=True, dry_run=False, **kwargs):
    try:
        Profile.objects.update_or_create(email=self.email)
    except Exception as e:
        print(e, file=sys.stderr)

但它产生了这个错误:

Exception Value: 'NoneType' object has no attribute 'import_type'

任何关于修复的见解将不胜感激。

【问题讨论】:

    标签: django django-import-export


    【解决方案1】:
    #try this 
    
    email = models.EmailField(max_length=200, unique=True, null=True)
    
            try:
                self.email = row["email"] 
            except Exception as e:
                self.email = None
    

    unique=True也屏蔽了重复的“”,但是null没有被屏蔽。

    【讨论】:

      【解决方案2】:

      请注意,您的 ProfileResource 上有这一行

      import_id_fields = ('id', 'email')

      这意味着导入将查看导入文件中的每一行,并尝试加载与id 和email 匹配的任何Profile 实例。

      因此,如果您只需要更新email,则只需将其从import_id_fields 列表中删除即可。通过这样做,逻辑将仅基于id 唯一标识实例,然后如果它们不存在,将创建新记录,并且将更新任何现有记录(包括更新电子邮件地址)。

      import-export 应该是这样工作的,不需要进一步的逻辑。

      如果email 是与现有实例匹配的“唯一字段”,那么这应该是import_id_fields 中的唯一字段。不能有两个Profile 实例具有相同的电子邮件,因为unique=True,因此您可以使用email 作为import_id_field。然而,使用import-export 更新Profile 电子邮件是不可能的(尽管您可以引入一个名为new_email 的字段并有逻辑以这种方式更新Profile,但这是一个单独的主题)。

      我不知道您为什么会看到“电子邮件已存在”错误 - 可能还有其他事情发生,可能是 before_import_row() 中的逻辑导致了问题,可能是因为正在使用空字符串作为电子邮件创建行。

      顺便说一句 - 你不应该需要 before_import_row() 中的任何逻辑 - import-export 应该为你处理这个问题,并且通过覆盖你可能会引入更多错误。

      使用import-export 的最佳建议是在您的IDE 中设置断点,然后在导入期间单步执行,您可以确切地看到发生了什么。从长远来看,这将为您节省大量时间。

      总之,在import_id_fields 中使用either id 或email 应该可以工作。

      【讨论】:

      • 只有在 id 字段为空时才会出现“电子邮件已存在”错误。如果 id 的值存在,它会更新得很好。我想它给出了那个错误,因为它是创建而不是更新。
      • 正确 - 它将尝试使用给定的电子邮件地址创建一个新行 - 如果它已经存在,您将看到错误。
      【解决方案3】:

      你应该尝试改变

      import_id_fields = ('id', 'email')
      

      通过

      import_id_fields = ('email',)
      

      当您尝试导入 ID 为空的行时,它将始终被视为新行。所以在这种情况下它会创建而不是更新。但是当你在import_id_fields 中只留下email 时,它显然会更新而不是创建。

      你已经试过了吗?

      【讨论】:

        猜你喜欢
        • 2014-04-29
        • 2021-12-12
        • 2014-06-11
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多