Description
Hello! I just want to describe something that has led me to confusion today.
The problem is this statement:
If you set the resource_name on a combination of model, serializer, or view in the same hierarchy, the name will be resolved as following: view > serializer > model. (Ex: A view resource_name will always override a resource_name specified on a serializer or model)
Say you have the following:
class PassengerSerializer(serializer.ModelSerializer):
class Meta:
resource_name = 'passenger-type-serializer'
model = Passenger
class CarSerializer(serializer.ModelSerializer):
passengers = ResourceRelatedField(many=True, source='passengers', read_only=True)
include_serializers = dict(passengers=PassengerSerializer)
class Meta:
model = Car
class Passenger(viewsets.ReadOnlyModelViewSet):
resource_name = 'passenger-type-view'
serializer_class = PassengerSerializer
class Car(viewsets.ReadOnlyModelViewSet):
serializer_class = CarSerializer
And you have a url configuration such that these resources are available through /cars/
and /passengers/
Doing:
GET /passengers/1/
will return type=passenger-type-view
while:
GET /cars/1/?include=passengers
will return an included
key with a list of resources with type=passenger-type-serializer
This is a bit inconsistent as depending from where you 'read' the passenger resource you might get different values for the type
key.
To fix this, the ResourceRelatedField
should be aware of the view the resource is exposed through, so that the resource_name
value from the view could be read. This is not possible.
What do you think? Maybe it's not the best idea to allow people to define resource names in the views and that should just stay in the serializers/models.
Cheers!