Answer the question
In order to leave comments, you need to log in
Django Rest Framework, when creating a user, how to specify a link to it in the user account model?
Hello, I am creating a user.
class RegisterSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'password')
def create(self, validated_data):
"""
Create the object.
:param validated_data: string
"""
user = User.objects.create(**validated_data)
user.save()
return user
class RegisterView(mixins.CreateModelMixin, mixins.RetrieveModelMixin,
mixins.UpdateModelMixin, viewsets.GenericViewSet):
queryset = User.objects.all()
serializer_class = RegisterSerializer
authentication_classes = ()
def post(self, request):
"""User registration view."""
user = request.user
user_account = UserAccount.objects.create(user=user.id)
user_account.save()
return self.create(request)
router = DefaultRouter()
router.register(r'register', RegisterView)
urlpatterns = router.urls
user = models.OneToOneField(User, on_delete=models.CASCADE)
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