Skip to content

using with django rest framework #34

Open
@hosamhamdy258

Description

@hosamhamdy258

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions