1 from django.db import models
2 from django.contrib.contenttypes.models import ContentType
3 from django.contrib.contenttypes import generic
4 from philo.models.base import value_content_type_limiter, register_value_model
5 from philo.utils import fattr
8 class Collection(models.Model):
9 name = models.CharField(max_length=255)
10 description = models.TextField(blank=True, null=True)
12 @fattr(short_description='Members')
14 return self.members.count()
16 def __unicode__(self):
23 class CollectionMemberManager(models.Manager):
24 use_for_related_fields = True
26 def with_model(self, model):
27 return model._default_manager.filter(pk__in=self.filter(member_content_type=ContentType.objects.get_for_model(model)).values_list('member_object_id', flat=True))
30 class CollectionMember(models.Model):
31 objects = CollectionMemberManager()
32 collection = models.ForeignKey(Collection, related_name='members')
33 index = models.PositiveIntegerField(verbose_name='Index', help_text='This will determine the ordering of the item within the collection. (Optional)', null=True, blank=True)
34 member_content_type = models.ForeignKey(ContentType, limit_choices_to=value_content_type_limiter, verbose_name='Member type')
35 member_object_id = models.PositiveIntegerField(verbose_name='Member ID')
36 member = generic.GenericForeignKey('member_content_type', 'member_object_id')
38 def __unicode__(self):
39 return u'%s - %s' % (self.collection, self.member)
45 register_value_model(Collection)