New member
- Joined
- Feb 2, 2023
- Messages
- 7
- Thread Author
- #1
When the book owner is authenticated it's ok, but if the book owner is not authenticated this error appears ValueError: Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x000002677F18EE00>": "Book.owner" must be an instance of " CustomUser".. I think this problem is caused by HyperlinkedModelSerializer. The owner of the book is specified in the perform_create() method, which is taken from self.request.user. I want authenticated users or non-authenticated users to be able to create books.
models.py
serializers.py
views.py
I need your help to fix this problem. Thanks in advance.
models.py
Code:
class Book(models.Model):
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True,
null=True, # allow Anonymous user
on_delete=models.CASCADE,
related_name="books",
)
title = models.TextField()
serializers.py
Code:
class BookSerializer(serializers.HyperlinkedModelSerializer):
owner_username = serializers.ReadOnlyField(source="owner.username")
class Meta:
model = Book
fields = (
"url",
"owner",
"owner_username",
"title",
)
read_only_fields = ("owner",)
Code:
class BookViewSet(viewsets.ModelViewSet):
serializer_class = BookSerializer
queryset = Book.objects.all()
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
I need your help to fix this problem. Thanks in advance.