Serializer.py***
class jobglobeluser_serializer(serializers.ModelSerializer):
class Meta:
model = jobglobeluser
fields = '__all__'
useris = serializers.ChoiceField(label='I Am:',
choices=USER_TYPE_CHOICES,
style={'class': 'useris'},
help_text='Choose Jobseeker Or Recruiter')
sector_type = serializers.ChoiceField(label='Sector Type',
required=False,
choices=SECTOR_TYPE,
style={'class': 'sector_type1'})
FirstName = serializers.CharField(
error_messages={'required': 'First Name Required'},
label='First Name',
style={
'placeholder': 'Your First Name',
'class': 'input1'
})
LastName = serializers.CharField(
error_messages={'required': 'Last Name Required'},
label='Last Name',
style={
'placeholder': 'Your Last Name',
'class': 'input2'
})
email = serializers.EmailField(error_messages={'required': 'Email Id Required'},
label='Email Id',
style={
'placeholder': 'Your Email ID',
'class': 'input3'
})
code = serializers.CharField(
initial='(+3455)',
label='Code',
style={'class': 'countrycode'})
mobile = serializers.CharField(
error_messages={'required': 'Mobile Number Required'},
label='Mobile',
style={
'placeholder': 'Your Mobile Number',
'class': 'input4'
})
country = serializers.ChoiceField(label='Country',
choices=COUNTRY,
style={'class': 'select'},
help_text='Choose Country First')
state = serializers.ChoiceField(label='State',
choices=STATES,
style={'class': 'select2'})
city = serializers.ChoiceField(label='City',
choices=CITY,
style={'class': 'select3'})
Gender = serializers.ChoiceField(error_messages={'required': 'Select Gender'},
label='Gender :- ',
choices=CHOICES, style={'class': 'radio1'})
password = serializers.CharField(
error_messages={'required': 'Choose Password'},
label='Password',
style={
'placeholder': 'Choose Password',
'class': 'input5',
'input_type': 'password'
})
password2 = serializers.CharField(
label='Reenter Password',
style={
'placeholder': 'Choose Same Password Again',
'class': 'input6',
'input_type': 'password'
})
def validate(self, data):
valctn = 'Select'
email_exists = jobglobeluser.objects.filter(email=email)
mobile_exists = jobglobeluser.objects.filter(mobile=mobile)
useris = data.get('useris')
sector_type = data.get('sector_type')
FirstName = data.get('FirstName')
LastName = data.get('LastName')
email = data.get('email')
mobile = data.get('mobile')
country = data.get('country')
state = data.get('state')
city = data.get('city')
password = data.get('password')
password2 = data.get('password2')
if useris == valctn:
raise serializers.ValidationError(
{'useris': 'Select Jobseeker Or Recruiter'})
if useris == 'Recruiter':
if sector_type == 'Select Sector Type':
raise serializers.ValidationError(
{"sector_type": 'Select Sector Type'})
if len(FirstName) < 3:
raise serializers.ValidationError(
{'FirstName': 'Name Is Too Short'})
if LastName == FirstName:
raise serializers.ValidationError(
{'LastName': "First & Last Name Should'nt Be Same"})
if email_exists:
raise serializers.ValidationError(
{'email': 'User Already Registered : Try To Reset Password'})
if len(mobile) < 8:
raise serializers.ValidationError({'mobile': 'Mobile Too Short'})
if len(mobile) > 13:
raise serializers.ValidationError({'mobile': 'Mobile Too Long'})
if mobile_exists:
raise serializers.ValidationError(
{'mobile': 'Mobile Already Registered : Enter Another One'})
if len(password) < 8:
raise serializers.ValidationError(
{'password': 'Password Must Contain 8 Characters'})
if password2 != password:
raise serializers.ValidationError(
{'password2': 'Password Does Not Match'})
if country == valctn:
raise serializers.ValidationError({'country': 'Select Country'})
if state == valctn:
raise serializers.ValidationError({'state': 'Select State'})
if city == valctn:
raise serializers.ValidationError({'city': 'Select City'})
return data
*View.py
class signup(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'usersignup.html'
style = {'template_pack':'rest_framework/vertical/'}
def get(self,request):
serializer = jobglobeluser_serializer()
return Response({'form': serializer})
def post(self, request):
serializer = jobglobeluser_serializer(data=request.data)
if not serializer.is_valid():
return Response({'form': serializer})
serializer.save()
return Response({'form': serializer})