1

我想修改 STRUCTURE_CHOICES 以便它可以反映到结构字段选择中。

类 AbstractProduct(models.Model):

...

  STRUCTURE_CHOICES = (
      (STANDALONE, _('Stand-alone product')),
      (PARENT, _('Parent product')),
      (CHILD, _('Child product'))
  )

...

  structure = models.CharField(
      _("Product structure"), max_length=10, choices=STRUCTURE_CHOICES,
      default=STANDALONE)

...

类产品(抽象产品):

...

  STRUCTURE_CHOICES = (
    (STANDALONE, _('Stand-alone product')),
    (PARENT, _('Parent product')),
    (CHILD, _('Child product')),
    (NEW_CHOICE, _('New Choice'))
  )

...

我尝试以这种方式进行操作,但没有成功:

类产品(抽象产品):...

  def __init__(self, *args, **kwargs):

     self.STRUCTURE_CHOICES = (
        (STANDALONE, _('Stand-alone product')),
        (PARENT, _('Parent product')),
        (CHILD, _('Child product')),
        (NEW_CHOICE, _('New Choice'))
     )
     return super(Product, self).__init__(*args, **kwargs)

...

4

1 回答 1

0

选项在定义时分配给字段,而不是在运行时。覆盖该STRUCTURE_CHOICES属性不会以任何方式影响该字段。但是,您可以做的是修改现有字段的选择:

class Product(AbstractProduct):
    pass

structure_field = Product._meta.get_field('structure')
structure_field.choices = …
于 2015-09-14T10:59:53.667 回答