Last active
August 29, 2015 13:56
-
-
Save asafge/8845569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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