Skip to content

Instantly share code, notes, and snippets.

@asafge
Last active August 29, 2015 13:56
Show Gist options
  • Save asafge/8845569 to your computer and use it in GitHub Desktop.
Save asafge/8845569 to your computer and use it in GitHub Desktop.
from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
class GenericLinkedModel(models.Model):
"""
Abstract base model that allows for an object to be linked (foreign-keyed)
to various models. Useful example: Comments or tags for different types of
objects.
Example Usage:
==============
from django.contrib.contenttypes import generic
...
class Comment(GenericLinkedModel):
text = models.Charfield(max_length=500)
...
class Post(models.Model):
comments = generic.GenericRelation(Comment)
...
"""
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Meta:
abstract = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment