Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb26764

Browse files
Anton-Shutikjerel
authored andcommitted
Added get_related_field_name method to RelationshipView
* Added get_related_field_name method to RelationshipView * Added docs about field_name_mapping
1 parent 05d8ef4 commit bb26764

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

‎docs/usage.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,15 @@ model field `Order.line_items` on the Order with pk 3, the url would be
413413
`self_link_view_name` keyword argument, which should match the `name=`
414414
provided in the urlconf, and will use the name of the field for the
415415
`related_field` kwarg.
416+
Also we can override `related_field` in the url. Let's say we want the url to be:
417+
`/order/3/relationships/order_items` - all we need to do is just add `field_name_mapping`
418+
dict to the class:
419+
```python
420+
field_name_mapping = {
421+
'line_items': 'order_items'
422+
}
423+
```
424+
416425

417426
### Meta
418427

‎rest_framework_json_api/views.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class RelationshipView(generics.GenericAPIView):
1919
serializer_class = ResourceIdentifierObjectSerializer
2020
self_link_view_name = None
2121
related_link_view_name = None
22+
field_name_mapping = {}
2223

2324
def get_serializer_class(self):
2425
if getattr(self, 'action', False) is None:
@@ -96,7 +97,7 @@ def patch(self, request, *args, **kwargs):
9697
related_model_class = related_instance_or_manager.__class__
9798
serializer = self.get_serializer(data=request.data, model_class=related_model_class)
9899
serializer.is_valid(raise_exception=True)
99-
setattr(parent_obj, kwargs['related_field'], serializer.validated_data)
100+
setattr(parent_obj, self.get_related_field_name(), serializer.validated_data)
100101
parent_obj.save()
101102
result_serializer = self._instantiate_serializer(related_instance_or_manager)
102103
return Response(result_serializer.data)
@@ -138,10 +139,16 @@ def delete(self, request, *args, **kwargs):
138139

139140
def get_related_instance(self):
140141
try:
141-
return getattr(self.get_object(), self.kwargs['related_field'])
142+
return getattr(self.get_object(), self.get_related_field_name())
142143
except AttributeError:
143144
raise NotFound
144145

146+
def get_related_field_name(self):
147+
field_name = self.kwargs['related_field']
148+
if field_name in self.field_name_mapping:
149+
return self.field_name_mapping[field_name]
150+
return field_name
151+
145152
def _instantiate_serializer(self, instance):
146153
if isinstance(instance, Model) or instance is None:
147154
return self.get_serializer(instance=instance)
@@ -153,7 +160,7 @@ def _instantiate_serializer(self, instance):
153160

154161
def get_resource_name(self):
155162
if not hasattr(self, '_resource_name'):
156-
instance = getattr(self.get_object(), self.kwargs['related_field'])
163+
instance = getattr(self.get_object(), self.get_related_field_name())
157164
self._resource_name = get_resource_type_from_instance(instance)
158165
return self._resource_name
159166

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /