Nested routes allow creation of objects for another parent object
See original GitHub issueSo, consider two models as example:
class User(AbstractUser):
pass
class Account(Model):
user = ForeignKey(settings.AUTH_USER_MODEL)
avatar = URLField()
This router:
router.register(r"users", UserViewSet) \
.register(r"accounts", AccountViewSet, base_name="users-accounts",
parents_query_lookups=["user"])
Now if you issue a POST
to /users/1/accounts/
with {"user": 2, "avatar": "www.example.com"}
the account is created for user with ID 2 instead ID 1.
Serializers (which create the stuff in DB) will use request.data
and request.data
have {"user": "2"}
. DRF-Extensions need to force the correct user in this situations.
One workaround to this problem is inject correct user in request.data
, something like:
class NestedAccountViewSet(NestedViewSetMixin, AccountViewSet):
def initialize_request(self, request, *args, **kwargs):
"""
Inject user from url in request.data
"""
request = super(NestedAccountViewSet, self).initialize_request(
request, *args, **kwargs
)
if request.data:
request.data["user"] = kwargs["parent_loookup_user"]
return request
Change the request.data
like code above is easy, but I’m sure that isn’t the best way to do it. Maybe DRF-Extensions can offer some custom serializer that receive extra information from url regex or do some magic in views so we don’t need to do this injection?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:12
Top Results From Across the Web
The Guide to Nested Routes with React Router - ui.dev
In this comprehensive, up-to-date guide, you'll learn everything you need to know about creating nested routes with React Router.
Read more >A Guide to Using Nested Routes in Ruby | Scout APM Blog
A guide detailing how Ruby simplifies the system of creating and managing routes and resources.
Read more >reactjs - React Router Nested Route renders inside of parent ...
@KonradLinkowski I tried doing it without the nesting but the Link needs a fixed path, ie /products/name-of-product. How do I change the Link...
Read more >React Router: A simple tutorial on nested routes.
So, we will create an array of objects, where each object will ... Till now, we have created some nested routes but those...
Read more >How To Create Nested Resources for a Ruby on Rails ...
Step 1 — Scaffolding the Nested Model · Step 2 — Specifying Nested Routes and Associations for the Parent Model · Step 3...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m facing a similar problem, which is that the parent object shouldn’t have to be specified in the request data, given it’s in the URL already. I’m addressing it by adding to NestedViewSetMixin:
This probably doesn’t correctly handle all possible configurations, but it’s working for mine.
Improved solution based on @Place1 's, work under Python 3.7 with Django 2.2 and DRF 3.10.
The line
field, lookup = key.split('__', 1)
is deleted as would cause error if no slug is given, since I currently only use primary key for index, it fits my minimal requirement.Feel free to use it and add your own modification.
At the end, I don’t feel like nested router is a good idea, nested view set make more sense for me. But I don’t really have time to switch to other solution right now.
Edit: If you’re reading this, please keep in mind that nested route/viewset for resources is a bad practice when designing RESTful API, and you should generally avoid doing this. Nesting resources would result unnecessary complication for dependencies, instead, you should use hyperlink for related resources.
Refer here for details about best practice of RESTful API design.