How to validate empty field value in django 'import_export' at db level

Hello everyone I have created one webpage where I am using Django ‘import_export’ Resource for .xlsfile upload. I am having below fields name in the .xls file as shown below image.

Now here id is my primary key and upload functionality is working fine. Now I want that when the user is uploading details AConnectID should be checked/validated and if it is blank then the process should stop.

I have tried using null=False and validator but it is working at the form level.

AConnectID = models.CharField(max_length=100, null=False,validators= [validators.MinLengthValidator(3)])

Any help on this how-to validates it?.

resource.py

class CTAResource(resources.ModelResource):

 class meta: Model=CTA

Hi Sorry for late reply, you can define you excel level field in your resource class like -

from import_export import resources,widgets,fields

class CTAResource(resources.ModelResource):
AConnectID = fields.Field(column_name=‘AConnectID’, attribute=‘AConnectID’, saves_null_values=False)
class meta: Model=CTA

1 Like

Hi Viny_Maurya,

Thanks, I have used It but no luck. As I am not getting any error and it is accepting empty value.

Hi Shailesh ,
In that case you should use import_row() method in your resource file,there you can handle your null value error . You can import each row of excel here by checking if value is not null .
pls refer to document
https://django-import-export.readthedocs.io/en/latest/api_resources.html

Hi Vinay,

Is it possible to count row from the .xlsx file by using before_import(by writing some logic/not sure how to write) and check if no of row let say more than 100 then do not upload it?

Yes it is possible to do that. The dataset argument contains a tablib dataset that you can manipulate:

def before_import(self, dataset, using_transactions, dry_run, **kwargs):
     number_of_rows = len(dataset)
1 Like