Open
Description
Could you explain how to use django-hashids with DRF correctly when trying to make post request consider this model and serialize files below
model.py
class Author(models.Model):
hashid = HashidsField()
name = models.CharField(max_length=255)
class Book(models.Model):
hashid = HashidsField()
name = models.CharField(max_length=255)
author= models.ForeignKey(Author, on_delete=models.CASCADE)
serilizers.py
class AuthorSerlizer(serializers.ModelSerializer):
id = serializers.CharField(source="hashid", read_only=True)
class Meta:
model = Author
fields = "__all__"
class BookSerlizer(serializers.ModelSerializer):
id = serializers.CharField(source="hashid", read_only=True)
author= serializers.PrimaryKeyRelatedField(
pk_field=serializers.CharField(source="hashid"),
queryset=Author.objects.all(),
)
class Meta:
model = Book
fields = "__all__"
payload = {
"author": "4JaYJV",
"name": "book1"
}
this payload raises
"author": ["Incorrect type. Expected pk value, received str."]}
Thanks in advance
Metadata
Metadata
Assignees
Labels
No labels