Answer the question
In order to leave comments, you need to log in
Django Rest Framework, how to properly implement serializers?
Good afternoon!
Let's say there are two models
class UserProfile(AbstractUser):
middle_name = models.CharField(max_length=1000, blank=True, null=True)
type = models.ForeignKey(UserType, blank=True, null=True, on_delete=models.SET_NULL)
class Driver(models.Model):
first_name = models.CharField(max_length=1000, blank=True, null=True)
middle_name = models.CharField(max_length=1000, blank=True, null=True)
last_name = models.CharField(max_length=1000, blank=True, null=True)
driver_license = models.CharField(max_length=1000, blank=True, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL)
class UserProfileReadSerializer(serializers.ModelSerializer):
type = UserTypeSerializer()
class Meta:
model = UserProfile
fields = ['id', 'username', 'last_name', 'first_name', 'middle_name', 'email', 'type', 'password', 'is_superuser', 'last_login', 'date_joined']
class DriverReadSerializer(serializers.ModelSerializer):
user = UserProfileReadSerializer()
class Meta:
model = Driver
fields = ['id', 'last_name', 'first_name', 'middle_name', 'driver_license', 'user']
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question