I'm using django-guardian and have 3 table that did not connected directly with foreign keys. Guardian_groupobjectpermission table can connected with any table by object_pk <-> table.pk Also Guardian_groupobjectpermission connected with auth_persmission by permission_id -> auth_permission.pk. Auth_persmission has codename field. Please look at table scheme image:
I need to get data from pybb_forum table that have codename = 'can_view'.
In sql this query will looks like:
mysql> select * from pybb_forum as ff join guardian_groupobjectpermission as gg on ff.id = gg.object_pk join auth_permission as pp on gg.permission_id = pp.id where codename = 'view_forum';
But i don't know have to create this query using django ORM. Is it possible ?
Something like
Forum.objects.filter(groupobjectpermission__permission__codename='view_forum')
?
Here is model code:
# django auth permission model:
class Permission(models.Model):
name = models.CharField(_('name'), max_length=50)
content_type = models.ForeignKey(ContentType)
codename = models.CharField(_('codename'), max_length=100)
# guardian group permission model
class GroupObjectPermissionBase(BaseObjectPermission):
permission = models.ForeignKey(Permission)
content_type = models.ForeignKey(ContentType)
object_pk = models.CharField(_('object ID'), max_length=255)
content_object = GenericForeignKey(fk_field='object_pk')
group = models.ForeignKey(Group) # reference to django group
class Forum(models.Model):
name = models.CharField(_('Name'), max_length=80, default='')
slug = models.SlugField(default='', unique=True)
hidden = models.BooleanField(_('Hidden'), blank=False, null=False, default=False)
headline = models.TextField(_('Headline'), blank=True, null=True)
class Meta(object):
permissions = (
('view_forum', 'Only view forum'),
)