Generic Many-To-Many Relationships

I have a collection class which contains arbitrary objects using a
Generic Many-To-Many relationship.


class Collection(models.Model):
    name = models.CharField(max_length=VERBOSE_NAME_MAX_LENGTH)

    def member_objects(self, klass):
        """Returns a QuerySet of all objects in the collection of the
given class."""
        # Should be stuff here.
        pass


class ObjectInCollection(models.Model):
    collection = models.ForeignKey(Collection)
    content_type = models.ForeignKey(ContentType)
    object_id =    models.PositiveIntegerField()
    object = generic.GenericForeignKey()


class ExampleObject(models.Model):
    # Stuff here
    # I don't want to rely on the following generic relation.
    oics = generic.GenericRelation(ObjectInCollection,
                                   related_name='example_object')


My problem is the member_objects method.  If I give all the object
classes that might be included in the collection a GenericRelation (as
in ExampleObject) then I can use:

   def member_objects(self, klass)
       return klass.objects.filter(oics__collection__id=self.id)

But I don't want to rely on having this in all the classes (because
the classes might be in other applications or there might be multiple
collection classes).

I could use a klass.objects.raw custom SQL method but that wouldn't
return a QuerySet which I would prefer.

Is there a way to do this that:
  1) does not require a GenericRelation in all classes and
  2) returns a QuerySet?

Cheers.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to