I am using Django import and export and importing data from .xls file and now I want to validate the col 2 value should not be empty for that I have used the below code but it is working for the admin site and not for data which is uploaded from an external file.
class CTSResource(resources.ModelResource):
class meta:
Model=CTA
def before_import(self, dataset, using_transactions, dry_run, **kwargs):
for col in dataset:
if col[2] == '':
raise ValidationError('This field black. '
'Error in row with id = %s' % col[2])
How to apply this validation at .xls file level. Is it possible to check at the file processing level here is my upload view code.
def CTA_upload(request):
try:
if request.method == 'POST':
movie_resource = CTAResource()
##we will get data in movie_resources####
dataset = Dataset()
new_movie = request.FILES['file']
if not new_movie.name.endswith('xls'):
messages.info(request, 'Sorry Wrong File Format.Please Upload valid format')
return render(request, 'apple/uploadinfosys.html')
messages.info(request, 'Uploading Data Line by Line...')
imported_data = dataset.load(new_movie.read(), format='xls')
count = 1
for data in imported_data:
value = CTA(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
data[8],
)
count = count + 1
value.save()
# messages.info(request, count)
# time.sleep(1)
messages.info(request, 'File Uploaded Successfully...')
except:
messages.info(request,'Same Email ID has been observed more than once.Except that other records has been added../nPlease Make sure Email field should be unique.')
return render(request,'app/cta.html')
How to do field level validaton.