class PropertyCategory(models.Model):
name = models.CharField(max_length=75)
def __unicode__(self):
return self.name
class Meta:
verbose_name = _('Property Category')
verbose_name_plural = _('Property Categories')
class Property(models.Model):
category = models.ForeignKey(PropertyCategory)
name = models.CharField(max_length=75)
def __unicode__(self):
return self.name
class Meta:
verbose_name_plural = 'Properties'
class Variant(models.Model):
properties = models.ManyToManyField(Property)
code = models.CharField(max_length=255)
def __unicode__(self):
product = Product.objects.get(variants__id=self.id)
return '%s (%s)' % (product.name, ', '.join([c.name for c in self.properties.all()]))
如何验证我的 Variant on Creation/Update 它只能有一个 Property
来自同一个PropertyCategory
.
假设我有这个产品;Lace-trim Cheeky Panty (Pink, XS)
, 其中括号之间的属性是Properties
fromPropertyCategory
Color
和Size
. 我想限制Variant
只能拥有一个PropertyCategory
,所以这样的事情不会发生;
Lace-trim Cheeky Panty (Pink, XS, Blue, M)
除此之外,我想知道是否可以设置一个特殊值是否PropertyCategory
允许有多个。假设洗涤说明可以出现多次。我将如何捕捉到这种差异?
class PropertyCategory(models.Model):
name = models.CharField(max_length=75)
multi_instance = models.BooleanField() # This determines if the Category's Properties can be applied more than once on a Variant
...
..然而,这个功能可能会带来更多问题,因为如果用户稍后决定设置multi_instance
from True
toFalse
等,它可能会破坏关系。
也许我可以PropertyCategory
用MultiPropertyCategory
?