Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Resource name defined in view is not used in related relationships #207

Closed
@jesushernandez

Description

@jesushernandez

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions