Python Orator ORM仅通过保存方法创建模型

时间:2019-03-22 09:51:35

标签: python

我希望有人可以向我解释为什么create方法没有在数据库中创建记录,但是如果我实例化模型,然后save它将在数据库中保留。 / p>

使用create方法时,出现以下错误。

TypeError: __init__() got an unexpected keyword argument 'postcode'

# Not working
location = Location.create(postcode="TS1 3ST")

# Working
location = Location()
location.postcode = "TS1 3ST"
location.save()

我在做什么错了?

谢谢

1 个答案:

答案 0 :(得分:0)

从.09版开始: 如文档中所述

  

您还可以使用create方法将模型保存在一行中,   但您需要指定fillable或guarded属性   在模型上,因为所有模型都受到保护,以防止   默认。

您必须在模型中添加:

class Location(Model):
    __fillable__ = ['postcode']

参考: https://github.com/sdispater/orator/issues/302