如何在 django 导入导出中显示清理过的电子邮件数据
How to display cleaned email data in django import-export
我有这个小部件:
class EmailWidget(CharWidget):
def clean(self, value, row=None, *args, **kwargs):
if value:
email_field = EmailField()
if email_field.clean(value):
return value
return None
我在这里使用:
some_email= fields.Field(attribute='some_email',
widget=EmailWidget(),
column_name='some_email')
清理没问题,但在错误的table视图上没有指定要清理的数据。
我期待的是这样的:
我只需要指定它并显示在第二行第二列的错误列表中。
我认为您没有正确实现小部件。来自 clean()
上的 docs:
Returns an appropriate Python object for an imported value.
For example, if you import a value from a spreadsheet, clean() handles conversion of this value into the corresponding Python object.
Numbers or dates can be cleaned to their respective data types and don’t have to be imported as Strings.
这意味着 clean()
不需要执行任何额外的验证。对于电子邮件字段,它只需要 return 一个字符串对象,CharWidget
将为您完成。
不过,有一种更简单的方法可以实现您想要的效果。如果您的模型字段在您的资源上声明为 EmailField, then you just need to set the clean_model_instances
元属性,并且电子邮件将在导入期间自动验证。
class Book(models.Model):
author_email = models.EmailField('Author email', max_length=75, blank=True)
class BookResource(ModelResource):
class Meta:
model = Book
clean_model_instances = True
使用示例应用程序进行测试时,输出为:
我有这个小部件:
class EmailWidget(CharWidget):
def clean(self, value, row=None, *args, **kwargs):
if value:
email_field = EmailField()
if email_field.clean(value):
return value
return None
我在这里使用:
some_email= fields.Field(attribute='some_email',
widget=EmailWidget(),
column_name='some_email')
清理没问题,但在错误的table视图上没有指定要清理的数据。
我期待的是这样的:
我只需要指定它并显示在第二行第二列的错误列表中。
我认为您没有正确实现小部件。来自 clean()
上的 docs:
Returns an appropriate Python object for an imported value.
For example, if you import a value from a spreadsheet, clean() handles conversion of this value into the corresponding Python object.
Numbers or dates can be cleaned to their respective data types and don’t have to be imported as Strings.
这意味着 clean()
不需要执行任何额外的验证。对于电子邮件字段,它只需要 return 一个字符串对象,CharWidget
将为您完成。
不过,有一种更简单的方法可以实现您想要的效果。如果您的模型字段在您的资源上声明为 EmailField, then you just need to set the clean_model_instances
元属性,并且电子邮件将在导入期间自动验证。
class Book(models.Model):
author_email = models.EmailField('Author email', max_length=75, blank=True)
class BookResource(ModelResource):
class Meta:
model = Book
clean_model_instances = True
使用示例应用程序进行测试时,输出为: