I have a ModelSerializer which I'm using to create new posts. It has a field book, of type PrimaryKeyRelatedField(queryset=Book.objects.all(), pk_field=serializers.CharField(max_length=255)).
When I post to this endpoint I get the error:
{
"book": ["Incorrect type. Expected pk value, received str."]
}
How can this be, as my Primary Key is a CharField.
The thing that really throws me off is, that I tried circumventing this with a SlugRelatedField, but when I do this, I get a really long and weird error:
DataError at /api/content/posts/
value "9780241470466" is out of range for type integer
LINE 1: ...020-05-22T20:14:17.615205+00:00'::timestamptz, 1, '978024147..., I don't understand it at all, as I am not setting an integer.
Serializer Code:
class PostCreationSerializer(serializers.ModelSerializer):
book = serializers.PrimaryKeyRelatedField(queryset=Book.objects.all(), pk_field=serializers.CharField(max_length=255))
class Meta:
model = Post
fields = ['content', 'book', 'page', 'date_posted', 'user', 'id']
read_only_fields = ['date_posted', 'user', 'id']
Model Code:
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
pages = models.PositiveSmallIntegerField(null=True, blank=True)
image = models.URLField(null=True, blank=True)
date_published = models.CharField(max_length=4, null=True, blank=True)
publisher = models.CharField(max_length=140, null=True, blank=True)
isbn13 = models.CharField(max_length=13, primary_key=True)
objects = AutomaticISBNDBManager
def __str__(self):
return self.title
View Code:
class PostListCreate(UseAuthenticatedUserMixin, generics.ListCreateAPIView):
serializer_class = PostSerializer
queryset = Post.objects.order_by('-date_posted')
def get_serializer_class(self):
if self.request.method == 'POST':
return PostCreationSerializer
else:
return PostSerializer
Edit: The POST I'm sending:
{
"book": "9780241470466",
"content": "test",
"page": "10"
}
Note: the user and date_posted are set automatically.