user3214546
user3214546

Reputation: 6831

How can i change django rest default behaviour for primary keys

According to Docs

http://www.django-rest-framework.org/api-guide/serializers/

The default ModelSerializer uses primary keys for relationships

It means for relation ships by default it will use

PrimaryKeyRelatedField

Now i want that instead of PrimaryKeyRelatedField the django rest should use

CustomPrimaryKeyRelatedField by default.

I don't want to manually write that in my 30 serializers and just want that if its not defined then use CustomPrimaryKeyRelatedField

Which thing i need to override for that

Upvotes: 1

Views: 98

Answers (1)

jacekbj
jacekbj

Reputation: 631

If you want to override this behaviour without modifying your code, you need to mangle with package source code. I think it's sufficient to change serializer_related_field in ModelSerializer https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py.

A better approach would be to create a class in your project which inherits from ModelSerializer and overrides this single field, then replace in your project ModelSerializer with MyModelSerializer.

Upvotes: 1

Related Questions