customerwebsitebackend/api/serializers.py
vickytechkey 0ef971cec4
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 31s
initial commit
2026-08-14 18:58:18 +05:30

25 lines
1,007 B
Python

from rest_framework import serializers
from django.contrib.auth.models import User
from orders.models import CustomerProfile
class UserRegisterSerializer(serializers.ModelSerializer):
phone = serializers.CharField(write_only=True, required=False, allow_blank=True)
address = serializers.CharField(write_only=True, required=False, allow_blank=True)
password = serializers.CharField(write_only=True)
class Meta:
model = User
fields = ['username', 'email', 'password', 'phone', 'address']
def create(self, validated_data):
phone = validated_data.pop('phone', '')
address = validated_data.pop('address', '')
password = validated_data.pop('password')
user = User.objects.create_user(
username=validated_data['username'],
email=validated_data.get('email', ''),
password=password
)
CustomerProfile.objects.create(user=user, phone=phone, default_address=address)
return user