I have this widget:
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
which I use here:
some_email= fields.Field(attribute='some_email',
widget=EmailWidget(),
column_name='some_email')
cleaning is fine but it does not specify the data to be cleaned on the table view in errors.
what I was expecting was something like this:
I only need to specify it and display it among the list of errors in the second row and second column.
I think you are not implementing the widget correctly. From the docs on clean()
:
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.
This means that clean()
doesn't need to perform any additional validation. In the case of an email field, it would only need to return a string object, and CharWidget
will do that for you.
However there is a simpler way to achieve what you want. If your model field is declared as EmailField, then you just need to set the clean_model_instances
Meta attribute on your resource, and the email will be validated automatically during import.
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
When tested with the example application, the output is: