What is "lookup" argument of NestedSimpleRouter class?

693 views Asked by At

I'm using drf-nested-routers to create several nested API endpoints.

Let's take a look at the next piece of code in my routes.py file:

from rest_framework_nested.routers import SimpleRouter, NestedSimpleRoute

base_router = SimpleRouter()
base_router.register("categories", CategoryModelView)

dishes_types_router = NestedSimpleRouter(base_router, r"categories", lookup="category")
dishes_types_router.register(r"types_of_dishes", DishesTypeModelView, basename="dishes_type")

My question is: what does lookup="category" in the fourth row do?

1

There are 1 answers

0
curioushuman On BEST ANSWER

While going trough the source code of drf-nested-routers repository, I've found this explanation in the doc strings in both NestedSimpleRouter and NestedDefaultRouter:

lookup:
The regex variable that matches an instance of the parent-resource will be called '<parent-viewset.lookup_field>' In the example above, lookup=domain and the parent viewset looks up on 'pk' so the parent lookup regex will be 'domain_pk'. Default: 'nested' where is 1+parent_router.nest_count

The source code and the example that's talked about in the explanation.