Skip to content

How to filter models in view based on request.user? #233

Answered by simkimsia
simkimsia asked this question in Q&A
Discussion options

You must be logged in to vote

I think @bennylope answer would work too and is achieved in a single line.

My answer is from the opposite direction.

The reason the relation is called core_accountuser is only because i placed the accountuser under an app called core. Your actual relation name may vary.

def get_account_by_user(user: CustomUser) -> Account:
    """
    given CustomUser, get the associated Account via core_accountuser relation
    """
    return user.core_accountuser.first().organization

account = get_account_by_user(request.user)

websites = Website.objects.filter(account=account).all()

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@simkimsia
Comment options

@bennylope
Comment options

@simkimsia
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by simkimsia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants