I have this profile resource:
class ProfileResource(resources.ModelResource):
email = fields.Field(attribute='email',
widget=CharWidget(),
column_name='email')
class Meta:
model = Profile
clean_model_instances = True
import_id_fields = ('email',)
which validates the email when the profile is created. It works fine but when I use it as a foreign key like:
class InvestmentResource(resources.ModelResource):
email = fields.Field(attribute='email',
widget=ForeignKeyWidget(Profile, field='email'),
column_name='email')
class Meta:
model = Investment
clean_model_instances = True
import_id_fields = ('id',)
def before_import_row(self, row, row_number=None, **kwargs):
self.email = row["email"]
self.profile__firstname = row["firstname"]
self.profile__lastname = row["lastname"]
def after_import_instance(self, instance, new, row_number=None, **kwargs):
"""
Create any missing Profile entries prior to importing rows.
"""
try:
profile, created = Profile.objects.get_or_create(email=self.email)
profile.firstname = self.profile__firstname
profile.lastname = self.profile__lastname
profile.save()
instance.profile = profile
except Exception as e:
print(e, file=sys.stderr)
It doesn't validate the email anymore. I tried adding two widgets, ForeignKeyWidget and CharWidget for the email on InvestmentResource, but it didn't work.
How do I then validate the email inside the InvestmentResource?