How to validate xl data

33 views
Skip to first unread message

Prashanth Patelc

unread,
Feb 14, 2022, 1:37:23 AM2/14/22
to django...@googlegroups.com
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)

Sujata Aghor

unread,
Feb 15, 2022, 12:26:24 AM2/15/22
to Django users
Hi Prashant,
If you are talking about server side validations, then you can do that for each row/ cell values before inserting data into db.

Regards!

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMCU6CrvCJzSCNjGTQ%2BgWjZ9ZURC%3Dk3H304WZ6vdQ5w812sisA%40mail.gmail.com.


--

Thanks & Regards!
Sujata S. Aghor

Sebastian Jung

unread,
Feb 15, 2022, 2:20:45 AM2/15/22
to django...@googlegroups.com

Ryan Nowakowski

unread,
Feb 25, 2022, 5:24:01 PM2/25/22
to django...@googlegroups.com
On Mon, Feb 14, 2022 at 12:04:13PM +0530, Prashanth Patelc wrote:
> 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

You could use a Python library[1] to parse the Excel file and validate the
sheet values in a validator function[2].

[1] https://www.python-excel.org/
[2] https://docs.djangoproject.com/en/4.0/ref/validators/
Reply all
Reply to author
Forward
0 new messages