Basic GenericForeignKey Question

137 views
Skip to first unread message

TheBeardedTemplar

unread,
Sep 21, 2016, 10:41:34 AM9/21/16
to Django users
Hey all,

I'm just getting started with GenericForeignKeys and I've run into a small point of confusion. I'm implementing a very general permission system as follows:

class Permission(models.Model):
   
"""
    This stores permissions for a single object.
    """

   
#These 3 fields are used to implement a generic foreign key
    content_type
= models.ForeignKey(ContentType)
    object_id
= models.PositiveIntegerField()
    model
= GenericForeignKey()

    user
= models.ForeignKey(User)

   
#Permissions
    read
= models.BooleanField(default=False)
    write
= models.BooleanField(default=False)
   
delete = models.BooleanField(default=False)



I also have a check permission function as follows:
    def checkPermission(self, obj, user, action):
       
try:
            p
= Permission.objects.get(model=obj, user=user)
           
return getattr(p, action)
       
except ObjectPermission.DoesNotExist:
           
return False

Currently this fails because I'm trying to access the GenericForeignKey directly, and I get this error:
django.core.exceptions.FieldError: Field 'model' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.

I've spent a while reading about GenericRelations but I'm still not exactly sure how to go about this. The Django documentation shows using GenericRelation fields on the models that my Permission points to, but it can point to many many different things and I'm hoping to get a way to use this functionality without putting a GenericRelation on every possible linked to model.

Thanks!

ludovic coues

unread,
Sep 21, 2016, 11:11:44 AM9/21/16
to django...@googlegroups.com
Unrelated, do you have any reason to not reuse the existing django
permission system ?
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/9160074b-4b1c-490b-93d9-d607895ba4eb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

TheBeardedTemplar

unread,
Sep 21, 2016, 1:26:11 PM9/21/16
to Django users
Hey Ludovic,

I simplified things a lot to illustrate my particular issue, but our permission system is actually pretty complex - the permissions can apply to any individual object, and they are also hierarchical like those that would apply to a folder system. I found there were decent extensions to the Django permission system to handle object-level permissions, and hierarchical permissions, but not both.
Reply all
Reply to author
Forward
0 new messages