S
S
Sergey Alekseev2018-03-12 13:02:22
Django
Sergey Alekseev, 2018-03-12 13:02:22

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

Explain how to specify a link to the User model in the UserAccount model? One to one relationship established between models
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 question

Ask a Question

731 491 924 answers to any question