I would like to extend the user model with attributes that are specific to the application, in this case I am simply substituting them with extA and extB. I have made a model to extend the user model and I have created a simple serializer for that model. Now I'm not sure how to create the user with the extra fields.
models.py
class UserDetails(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
extA = models.IntegerField(null=True)
extB = models.IntegerField(null=True)
@receiver(post_save, sender=User)
def handle_user_save(sender, instance, created, **kwargs):
if created:
UserDetails.objects.create(user=instance)
serializers.py
class UserDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email', 'extA', 'extB')
views.py
class UserDetailsView(APIView):
def get(self, request, id, format=None):
data = User.objects.get(id)
serializer_class = UserDetailsSerializer
return Response(serializer_class.data)
I have been creating users via the shell. Like this:
user = User.objects.create_user('John', '[email protected]', 'pass'). This works really well but I'm not sure how to create a UserDetailsView object and save it.