I'm trying to convert from using Django forms to just using REST Framework serializers for the forms. My models are:
class UserDetails(models.Model):
user = models.OneToOneField(User, related_name='details', on_delete=models.CASCADE)
date_of_birth = models.DateField(default=None, blank=True, null=True)
class Employee(models.Model):
user = models.OneToOneField(User, related_name='employee', on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
class Company(models.Model):
name = models.CharField(verbose_name='Company Name', max_length=50, unique=True)
info = models.CharField(verbose_name='Company Information', max_length=300, blank=True)
I have serializers for User, UserDetails, and Company.
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.User
fields = ('id', 'username', 'email', 'first_name', 'last_name', )
class UserDetailsSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = models.UserDetails
fields = ('user', 'date_of_birth')
read_only_fields = ('is_verified',)
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = models.Company
fields = ('name', 'info')
When users register, they will also specify company information and will become an employee of that company. How do I go about creating a form to create all three records atomically?
Here's my attempt:
class RegistrationSerializer(serializers.Serializer):
details = UserDetailsSerializer(label='')
password = serializers.CharField(
label='Password',
style={'input_type': 'password'}
)
password2 = serializers.CharField(
label='Password Confirmation',
style={'input_type': 'password'}
)
company = CompanySerializer()
I also want a password confirmation field, which will cause the form to be invalid if it does not match. Django forms had a clean()
method which I used to valid the data. I'm not sure if serializers do (can't find it in the doucmentation). Also, when I call the serializer's save()
in my View class, it's asking me to implement the create()
method for the serializer.