类型对象'属性'没有属性选项'

时间:2015-12-17 06:54:03

标签: django django-models

你好,我试图拨打外键'选项' charfield是一个选择字段,但得到上述错误

这里是models.py

class Color(models.Model):
    product = models.ForeignKey(Product)
    name = models.ForeignKey('Attribute')
    value = models.CharField(max_length=100)

    objects = ColorManager()

    class Meta:
        unique_together = ('product' , 'value')

    def __unicode__(self):
        return self.name

class Attribute(models.Model):
    OPTION_CATEGORIES = (
        ('color' , 'color') , 
        ('size' , 'size') , 
        ('type' , 'type') , 
        ('sleeves' , 'sleeves') , 
        )
    option = models.CharField(max_length=100 , choices = OPTION_CATEGORIES)
    value = models.CharField(max_length=100)

    def __unicode__(self):
        return self.option

这里是views.py

Color.objects.get_or_create(product=instance ,name=Attribute.option.color , value=color)

这里是追溯

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\lenovo\Desktop\plump\Plumpin\src\products\views.py" in DataEntry
  287.                 Color.objects.get_or_create(product=instance ,name=Attribute.option.color , value=color)

Exception Type: AttributeError at /dataentry/dataentry
Exception Value: type object 'Attribute' has no attribute 'option'

属性表有一个名为option的属性,但它仍然给出了上述错误

1 个答案:

答案 0 :(得分:0)

来自你的追溯;

在views.py

Color.objects.get_or_create(product=instance ,name=Attribute.option.color , value=color)

这里的名字应该是一个对象,就像这样;

Color.objects.get_or_create(product=instance ,name=Attribute.objects.get(option='color') , value='color')