Add double underscore lookups for SlugRelatedField slug_field #8191
Unanswered
kkirss
asked this question in
Ideas & Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The SlugRelatedField allows specifying a
slug_field
to represent a relation by that field's value. I think that in some cases it would be useful to use a nested relation's field instead. I think it would make sense to use django's double underscore syntax for this.For example - lets say we have a site that has n organizations, which all have n blog posts. All users belong to one organization and some of them may be moderators. Everyone can create blog posts. Only users that originally created the blog post or the creator organizations moderators can edit the blog posts. We want the blog post serializer to return the creators username and the creators organization name. (Might not be the most practical example but you get the idea.)
Here's how you could write that site:
However, this doesn't currently work because the
slug_field="user__username"
will raise anAttributeError
inSlugRelatedField.to_representation
.My proposal is to allow this syntax. Here's also a reddit thread of someone asking if this would be possible. So seems I'm not the first person to think this would be useful.
The functionality should be really simple to add (need to add docs and tests for it too though):
I can make a PR for this if others think this would be useful.
Beta Was this translation helpful? Give feedback.
All reactions