Django: Example of generic relations using the contenttypes framework?
Asked Answered
E

3

21

I've pored over the Django docs regarding the contenttypes framework several times, and I simply don't understand it well enough to implement generic relations in my project. I've looked for online examples or tutorials regarding the matter, but I can't find a single one. Call me dumb, but I need some help on this one (please don't answer by simply linking to the docs). Based on the lack of resources online, I believe if you answer this question with a thorough example, your answer may be the most helpful example online to date regarding django generic relations (bonus!).

So, my question is: can someone show a simple example of the models and maybe a couple lines of code showing how to interact with instances of a generic model?


As inspiration, here is what I believe would be a very common situation:

A site has media items that are largely treated the same, but are slightly different. For example, let's say there are image and video items, and users can "like" an item or "comment" on an item. The likes and comments should be treated the same, regardless of whether they are posted on an image or video item. So, if there is an ItemView for viewing an image or a video in a user's album, the following kinds of calls would be possible : mediaitem.comments.all() or len(mediaitem.likes.all()) or comment.user_who_commented, without having to know which kind of media item it is (image or video).

I believe you would need six models for this:

  • Abstract MediaItem class
  • Two different types of media item: ImageItem and VideoItem
  • Abstract MediaItemActions class
  • Two different types of actions that can be done towards media items: Like and Comment

If you know how to use this Django feature, please show us a full example! I feel like it would be an extremely powerful tool and am aching to use it in my application. The more explicit, the better.

Edwin answered 6/6, 2013 at 3:5 Comment(0)
C
6

Your use case sounds very similar to the (now deprecated) Django comments framework. If you check out the models, you'll see how to use a generic relation in BaseCommentAbstractModel--note that you need all three fields, a ForeignKey to ContentType, a field to hold the objects' pks, and the GenericForeignKey field.

As for how to query for objects by GenericForeignKey, you can see some examples in the template tags in that project. See for example the get_query_set method in BaseCommentNode, which retrieves comments by querying on the content type and pk of the target object.

def get_query_set(self, context):
    ctype, object_pk = self.get_target_ctype_pk(context)
    if not object_pk:
        return self.comment_model.objects.none()

    qs = self.comment_model.objects.filter(
        content_type = ctype,
        object_pk    = smart_text(object_pk),
        site__pk     = settings.SITE_ID,
    )
Cayes answered 6/6, 2013 at 4:10 Comment(0)
S
3

I actually have a very similar situation on one of my projects, with various media types.

class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id') 

class ReviewedItem(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    review = models.ForeignKey("Review")

class CreativeWork(models.Model):
  #other fields 
  keywords = generic.GenericRelation("TaggedItem",null=True, blank=True, default=None)
  reviews = generic.GenericRelation("ReviewedItem",null=True, blank=True, default=None)

class MediaObject(CreativeWork):
  #fields
class VideoObject(MediaObject):
  #fields
class AudioObject(MediaObject):
  #fields

Every Video or Audio is a MediaObject, which is a CreativeWork. CreativeWorks have a GenericRelation to tags and Reviews. So now anything can be tagged or reviewed.

All you need is for the 'action' to have a ForeignKey to ContentType. Than add a GenericRelation to your model. I actually found the django.docs to be pretty helpful :) But if not hope this helps.

Sylvanite answered 23/3, 2015 at 15:12 Comment(0)
H
1

Another option is Polymorphic Models. I won't say it is the way you should go, but that it could perhaps be an option.

I am a fan of both generic foreign keys and Polymorphic Models. Polymorphic Models work best in those scenarios where there is a lot of similarity in the models.

Hulky answered 6/6, 2013 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.