Sunday, February 13, 2022

How to validate xl data

i am working on xl sheets , when im uploading the xl sheet it is storing into the models but i need before storing into the models validate the data

eg:
username : ,must be str not int
reference id : ,must be int not str
email : ,contains @gmail.com , not strt not int

i need to validate like above


this is code my code 
working but i need validations


class UploadPersonView(APIView): permission_classes = [IsAuthenticated, IsAdminPermission | IsSuperAdminPermission]
serializer_class = PersonUploadSerializer
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
dataset = Dataset()
file = serializer.validated_data['file']
imported_data = dataset.load(file.read(), format='xlsx')
'''uploading xl file with particular data what user mentioned in xl we are looping the xl data
and appending into the database with same fields''' # validate data here before upload
for data in imported_data:
person_data = UserProfile(username=d,
fullname=data[1],
mobile=d2,
email=data[3],
password=make_password(data[4]),
date_joined=data[5],
is_verified=data[6],
is_active=data[7],
is_admin=data[8],
is_manager=data[9],
is_tl=data[10],
is_agent=data[11],
orginization=data[12],
dob=data[13],
gender=data[14],
team_name_id=data[15],
role=data[16]
)
person_data.save()
return Response({'sucessfully uploaded your file'}, status=status.HTTP_200_OK)
except:
return Response({'message': 'aaa'}, status=status.HTTP_404_NOT_FOUND)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMCU6CrvCJzSCNjGTQ%2BgWjZ9ZURC%3Dk3H304WZ6vdQ5w812sisA%40mail.gmail.com.

No comments:

Post a Comment