【发布时间】: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