Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jesushernandez opened this issue Feb 16, 2016 · 1 comment
Closed

Comments

@jesushernandez
Copy link

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!

@jerel
Copy link
Member

jerel commented Feb 22, 2016

I'm hesitant to remove the possibility of setting resource_name on the view because there could be cases where it is unavoidable. Maybe the best fix would be to add a note to the docs saying something like "Setting the resource_name on the serializer or model is preferred as views are not used for determining the type of ResourceRelated fields."

Do you agree? I'd welcome a PR if you think that is the best fix.

@jerel jerel closed this as completed in 05d8ef4 Apr 15, 2016
jerel added a commit that referenced this issue Apr 29, 2016
* docs: note about importing serializers

* Document ResourceRelatedField and RelationshipView

* Updated pip install instructions for 2.0.0-beta.2

* Add LimitOffsetPagination

* Dont let the offset go into negative space

* Add basic unit test for LimitOffsetPagination

* Support deeply nested includes
Allow skipping of intermediate included models

* Add current tox.ini directory to PYTHONPATH in order to use imports form there
Fix regression on PY3 caused by unicode_literals

* [FEATURE]: support using get_serializer_class on view

* fixed extract_root_meta for lists

* Fixed get_resource_name in case of non-model backed serializer.

Closes #219

* ResourceRelatedField now accepts serializer methods when many=True

* Rename "suggested" posts to "featured" so we can use suggested as many=True

* Updated SerializerMethodResourceRelatedField to allow many=True

Issue #151
Closes #220

* Correct error responses for projects with different DRF-configurations (#222)

* [#214] Add error messages tests.

* [#214] Extract formatting DRF errors.

* Add example view with custom handle_exception.

* Use HTTP 422 for validation error responses.

* Add full example of class-configured json api view.

* Fixed naming that suggested settings were used to inflect relationship names.

JSON_API_FORMAT_RELATION_NAME actually inflected the `type` instead.
The relation name is not changable at this time although if it woudl
be useful to someone it would be fine to implement it.
Closes #136.

* Updated changelog

* Added a doc note to prefer setting resource_name on serializers or models.

Closes #207

* Added get_related_field_name method to RelationshipView

* Added get_related_field_name method to RelationshipView

* Added docs about field_name_mapping

* Updated the readme for testing (#234)

* Allow exception handler to be used by normal DRF views: (#233)

* Add top-level 'errors' object to non-JSON-API responses
* Allow configuring the exception handler to be used _only_
  in JSON API views or uniformly across all views

* Fix included resource type inconsistency (#229)

When setting `resource_name = None`, the related instance's resource
name is used in `relationships`, but `None` is used in `included`.

This is related to #94 and #124

* Fixes #230. Keep write only fields from having an attribute key

* Release v2.0.0

* Update setup.py to classify as production/stable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants