django模型多态与代理继承

aam*_*icz 7 python django proxy-classes django-models

我的Discount模型描述了系统中所有类型折扣的常用字段.我有一些代理模型描述了用于计算总数的具体算法.基Discount类有一个名为的成员字段type,它是一个标识其类型及其相关类的字符串.

class Discount(models.Model):
  TYPE_CHOICES = (
    ('V', 'Value'),
    ('P', 'Percentage'),
  )

  name = models.CharField(max_length=32)
  code = models.CharField(max_length=32)
  quantity = models.PositiveIntegerField()
  value = models.DecimalField(max_digits=4, decimal_places=2)
  type = models.CharField(max_length=1, choices=TYPE_CHOICES)

  def __unicode__(self):
    return self.name

  def __init__(self, *args, **kwargs):
    if self.type:
      self.__class__ = getattr(sys.modules[__name__], self.type + 'Discount')
    super(Discount, self).__init__(*args, **kwargs)

class ValueDiscount(Discount):
  class Meta:
    proxy = True

  def total(self, total):
    return total - self.value
Run Code Online (Sandbox Code Playgroud)

但我继续得到AttributeError的例外,说自己没有类型.如何解决这个问题还是有另一种方法来实现这一目标?

Der*_*wok 12

你的init方法需要看起来像这样:

def __init__(self, *args, **kwargs):
    super(Discount, self).__init__(*args, **kwargs)
    if self.type:
        self.__class__ = getattr(sys.modules[__name__], self.type + 'Discount')
Run Code Online (Sandbox Code Playgroud)

__init__在您能够访问之前,您需要调用super self.type.

type因为type也是一个python内置函数,因为调用你的字段很棒,尽管你可能不会遇到任何问题.

请参阅:http://docs.python.org/library/functions.html#type

  • 这是因为django.db.models.Model有`__metaclass__ = ModelBase`.这意味着django使用`ModelBase`来创建Model类而不是常规`type`.我强烈建议您阅读:http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python - 一旦您掌握了元类,请查看django源代码. (3认同)