I have created an Author model in which it has a OneToOne relationship with default Django User, as below:
from django.contrib.auth.models import User
from django.db import models
class Author(models.Model):
user = models.OneToOneField(
User,
related_name='author',
on_delete=models.CASCADE,
default="",
)
is_author = models.BooleanField(
default=True
)
And here I have created the following viewset:
class AuthorViewSet(viewsets.ModelViewSet):
serializer_class = AuthorSerializer
queryset = Author.objects.all()
def get_permissions(self):
if self.action == "list" or self.action == "retrieve" or self.action == "update":
self.permission_classes = [IsCurrentOwner, permissions.IsAdminUser]
elif self.action == "create":
self.permission_classes = [permissions.AllowAny]
return super(AuthorViewSet, self).get_permissions()
Question Is there any way that I can create both user and author in one step (request)? How?
serializer code:
class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = "__all__"
extra_kwargs = {
'password': {'write_only': True},
'id': {'read_only': True}
}
I have tried the following request but it doesn't work.
#url: localhost:8000/users/
#method: POST
{
"user": {
"username": "mostafa",
"password": "1"
}
}
Error:
{
"user": [
"Incorrect type. Expected pk value, received dict."
]
}