Skip to content

Instantly share code, notes, and snippets.

@victorfsf
Last active September 18, 2015 19:17
Show Gist options
  • Save victorfsf/c9e8ac2b6447c49d75c7 to your computer and use it in GitHub Desktop.
Save victorfsf/c9e8ac2b6447c49d75c7 to your computer and use it in GitHub Desktop.
Django's Generic Foreign Key Model Mixin
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.db import models
class GenericModelMixin(models.Model):
model_id = models.PositiveIntegerField(blank=True, null=True, editable=False)
model_type = models.ForeignKey(ContentType, blank=True, null=True, editable=False)
model_object = models.GenericForeignKey('model_type', 'model_id')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment