Skip to content
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

Update README.md #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ class DomainSerializer(HyperlinkedModelSerializer):
nameservers = DomainNameserverSerializers(many=True, read_only=True)
```

### Custom actions

If you have custom actions in your viewset, you'll need to include `**kwargs` to your method signature, like so:

```
class ParentViewSet(viewsets.ModelViewSet):
queryset = Parent.objects.all()
serializer_class = ParentSerializer

class ChildViewSet(viewsets.ModelViewSet):
def get_queryset(self):
return Child.objects.filter(parent=self.kwargs.get('parent'))

@action(methods=['get'], detail=False)
def do_something(self, request, **kwargs):
pass
```

Notice that in this case, the custom endpoint registered in the `ChildViewSet` is marked to be a list action (`detail=False`). It's usual signature, would be: `def do_something(self, request):`. If you maintain that signature, `drf-nested-routers` won't be able to inject the necessary parameters in the function. So add the `**kwargs` and it will work just fine.

The same applies for custom detail actions.

### Infinite-depth Nesting

Example of nested router 3 levels deep. You can use this same logic to nest routers as deep as you need. This example accomplishes the below URL patterns.
Expand Down