Reading the docs lead me to understand that the default permission system in Django is tied to models, which is understandable since it was created for the admin interface.
What does this mean for views? Would the model that's connected to the permission just be kind of not used if I'm only using the permission for views?
Here's to making this question a bit more specific. My goal is to limit a view to one specific group of users.
Create a ContentType:
content_type = ContentType.objects.get(app_label='myapp', model='SomeModel')
Create a Permission:
permission = Permission.objects.create(codename='can_view_this', name='Can Access this View', content_type=content_type)
Create a Group:
group = Group.objects.get_or_create(name='some_group')
Add a permission to the group:
group.permissions.add(permission)
Add a user to the group:
group.user_set.add(User.objects.get(id=1))
Test for the permission:
@user_passes_test(lambda u: u.has_perm('myapp.can_view_this'))
def some_view(request):
So going back to step #1, notice how I had to assign a model in order to create a ContentType, and I had to create a ContentType because creating a Permission entry requires a ContentType.
So the question is, once again, is that SomeModel
being used at all in this scenario? I'm not even sure what exactly which model to assign a view-specific permission, doesn't make any sense.
Hopefully the question makes sense.