i’m developing a authentication backend with object-based permissions for my django-app.I use generic relations between an object and a permission:
class GroupPermission(models.Model):
content_t= models.ForeignKey(ContentType,related_name='g_content_t')
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_t', 'object_id')
permission=models.ForeignKey(Permission)
group = models.ForeignKey(Group)
And now i want to get all objects of a specified content type for which a сertain group or user has a сertain permission. What’s the best way to do that?
Shall i define the second end of the relation in the app’s models? or better write custom sql?
I’m trying to build a generic backend so i don’t want it to depend on the app that is using it.
Thanks!
I’m guessing you’re looking for something like:
This should get all of the objects which have the Permission of
perm, and the Group ofgroupinto the variableobjects.You could implement this into a Custom Manager class, as well:
Which would make your view code simpler: