[Fixed]-How do you set Django permissions based on models linked to a user (and not based on a user's group)?

1👍

You’re spot on – you will need to define your own logic to see if the user has been approved, not permissions. Permissions don’t really allow you to test on a per-user or per-model basis the way you’re looking for.

There are two places to define where this test will be. Either in the view itself, or via your own @user_passes_test decorator. You already have a Foreign Key to the user object, so you can write a test in the view, like this:

@login_required
def survey_view(request):
    survey = Screening_survey.objects.filter(user=request.user)
    if survey and survey.is_accepted is 'yes':
        # Do the remaining view logic
    else:
        raise PermissionDenied

If you want to put this logic in the @user_passes_test, that would work well too. The user object is passed directly (see the docs for an example)

Leave a comment