Skip to content

Instantly share code, notes, and snippets.

@robgolding
Created June 24, 2011 16:53
Show Gist options
  • Save robgolding/1045185 to your computer and use it in GitHub Desktop.
Save robgolding/1045185 to your computer and use it in GitHub Desktop.
Custom Fields proposed implementation
from django.db import models
from django.contrib.contenttypes.models import ContentType
CUSTOMFIELD_TYPE_CHOICES = (
('bool', 'boolean'),
('char', 'text'),
('int', 'integer'),
('float', 'decimal'),
)
class CustomFieldManager(models.Manager):
def get_for_account_and_object(self, account, obj):
ctype = ContentType.objects.get_for_model(obj)
return self.filter(account=account, content_type=ctype)
class CustomField(models.Model):
account = models.ForeignKey('accounts.Account')
content_type = models.ForeignKey(ContentType)
name = models.CharField(max_length=255)
type = models.CharField(max_length=20, choices=CUSTOMFIELD_TYPE_CHOICES)
class Meta:
unique_together = ('account', 'content_type', 'name')
def __unicode__(self):
return '[CustomField] %s' % self.name
class CustomFieldValue(models.Model):
field = models.ForeignKey('customfields.CustomField')
value = models.CharField(max_length=255)
def __unicode__(self):
return self.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment