Skip to content

Instantly share code, notes, and snippets.

@bobobo1618
Created June 26, 2013 01:10
Show Gist options
  • Save bobobo1618/5863929 to your computer and use it in GitHub Desktop.
Save bobobo1618/5863929 to your computer and use it in GitHub Desktop.
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
# Create your models here.
class Barcode(models.Model):
""" A barcode can be thought of a real world generic foreign key that points to an object somewhere in the DB """
code = models.CharField(max_length=128, unique=True, blank=False)
content_type = models.ForeignKey(ContentType, blank=True, null=True)
object_id = models.PositiveIntegerField(blank=True, null=True)
content_object = generic.GenericForeignKey(ct_field='content_type', fk_field='object_id', null=True)
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
# This will get slow when viewing a large list of barcodes.
return "<Barcode: %s> [%s.%s] %s" % (self.code, self.content_type.app_label, self.content_object.__class__.__name__, self.content_object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment