nshafer / django-hashid-field

Django Model Field that uses Hashids to obscure the value
MIT License
370 stars 40 forks source link

HashidAutoField and REST post requests #12

Closed oliverbienert closed 7 years ago

oliverbienert commented 7 years ago

Hi, in Django Rest Framework, I normally don't have to declare id fields explicitly. When I post data, a new instance is created. Sadly, this does not work if I make my model id a HashidAutoField:

id = HashidAutoField(primary_key=True) 

In the ModelSerializer, I have to declare id explicitly as well:

id = HashidSerializerCharField(source_field='api.project.id')

When I now send a post, the server responds with an ApiError:

id: This field is required.

Naturally I would not send an id, for it is a post message, intended to create a new object. And I need the id to automatically created by the database, as usual. How is this supposed to work?

oliverbienert commented 7 years ago

My fault, sorry. It was quite late last night, shouldn't work that hours ;-) Forgot to set read_only=True on the serializer field:

id = HashidSerializerCharField(source_field='api.project.id', read_only=True)