-
Notifications
You must be signed in to change notification settings - Fork 160
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
Using same lookup kwarg for base and nested views #270
Comments
Hi, @Kangaroux. Thanks for reaching out. I see what you mean, but I did not knew that it leads to the need to create two permission classes :/ I will take a look on the DRF docs and if I find something I ping you. For today, no I have not a better idea 🤷. |
Hey @Kangaroux, I got some more understanding over the First, the lookup name cames from this pieces of code:
If you really want to get rid of the Conceptually, the parent lookup is about how to get the parent object from the child object! I mean: # models.py
class Company(models.Model):
...
class User(models.Model):
company = models.ForeignKey('Company', ...) That would make sense to produce:
As it ease the task to reach the Company from the User inside the View: User.objects.get(company_pk=self.kwargs['company_pk'], pk=self.kwargs['pk'])
# or more generically:
User.objects.get(**self.kwargs) Also, in the View for filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
obj = get_object_or_404(queryset, **filter_kwargs) then the <company_pk> is just unused. Hope this to helps to understand how and why things are as they are today, yet any change proposal will be very welcome from my side 👍 Thanks again, |
Suppose I have a resource
company
with a nested resourceusers
:I rely on the
company_pk
kwarg to check ifrequest.user
has permissions, but because the naming is different, I need to create two permission classes: one for nested views and one for the base resource.I tried setting
lookup_url_kwarg = "company_pk"
on theCompanyViewSet
but that generates the nested routes withcompany_company_pk
. Settinglookup=""
on the router generates_company_pk
.It's not a big deal to create a couple extra permission classes, or to make a custom router that generates the nested lookups like I want, but I wanted to know if this is possible with the library as-is
The text was updated successfully, but these errors were encountered: