Filter related models by authenticated users relation #7998
Unanswered
NoNameWhatever
asked this question in
Question & Answer
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.
-
Hi =)
I can't find any documentation about how to only get the related models objects based on the authenticated users relation.
I have three models. One for
User
, a custom user model with a M2M field to a model calledGroup
. The third model isPost
with a text field and a FK field to Group model.Users have membership to groups with the M2M field. I want them to only be able to post to these groups.
But all groups are in the select option when creating a post. I want to only send back the groups that the user is a member of.
Simplified models:
ViewSet like this.
And for serializer, this.
This setup makes authenticated user to choose every group in the whole db.
I'm trying to do so the groups selectable/sent to frontend is only the groups that the authenticated user are a member of by the M2M field in the user model.
Is it even possible to achieve this?
Beta Was this translation helpful? Give feedback.
All reactions