Saturday, June 21, 2014

How to filter generic foreign keys?

I have these models:

class EventEntry(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    start_date =  models.DateTimeField(...)
    end_date = models.DateTimeField(...)
    field = models.CharField(max_length=64)

class ObjectPerm(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    public = models.BooleanField(...)
    users = models.ManyToManyField(User...)
    groups = models.ManyToManyField(Group...)

class Meeting(models.Model):
    scheduled_time = generic.GenericRelation(EventEntry)
    room = models.CharField(max_length=8)


class Milestone(models.Model):
    due_date = generic.GenericRelation(Proof)
    title = models.CharField(max_length=128)

The EventEntry class is used to attach scheduling information to any object, such as Meeting and Milestone.

The ObjcetPerm class provides object level permissions to any model that needs protection.

When I create an instance of Meeting, I assign an EventEntry instance for the schedule information. I also assign ObjectPerm instances to specify which users can access the meeting object.

There are more models like Meeting and Milestone.

Now, what I want to do is retrieve EventEntry instances, but only those for related objects the user has permission to access, based on the ObjectPerm model. For example all EventEntry instances where public == True or the user is in the EventEntry field "users". This way I can pull out all the events to display in a calendar but I don't need to filter the meetings, milestones etc. Except I can't work out how to write this filter in Python using Django's ORM. Any suggestions appreciated. Thanks

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/822acdf7-a903-48b9-9557-ae12790e4c14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment