Django:为第三方课程创建模型

时间:2016-06-10 13:07:13

标签: python django multiple-inheritance

为现有的第三方(非Django)Python类添加Django模型有什么好方法?我尝试了多次继承:

class ThirdPartyClass(object):
    pass

class ModelForThirdPartyClass(models.Model, ThirdPartyClass):
    uid = models.ForeignKey(settings.AUTH_USER_MODEL)

但是失败了(Django 1.8.7):

File "/usr/lib/python3/dist-packages/django/db/models/base.py", line 361, in _prepare
    cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join(f.name for f in opts.fields))
TypeError: sequence item 4: expected str instance, int found

1 个答案:

答案 0 :(得分:0)

呃,我一直在追逐错误的方向:上面的多重继承代码实际上是有效的。错误是我将以下字段添加到ModelForThirdPartyClass:

foo = models.DecimalField(19, 4)

而我应该写

foo = models.DecimalField(max_digits=19, decimal_places=4)

由于在创建类时会对字段定义进行求值,因此我误解了与解析继承有关的错误。