我希望有人可以向我解释为什么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()
我在做什么错了?
谢谢
答案 0 :(得分:0)
从.09版开始: 如文档中所述
您还可以使用create方法将模型保存在一行中, 但您需要指定fillable或guarded属性 在模型上,因为所有模型都受到保护,以防止 默认。
您必须在模型中添加:
class Location(Model):
__fillable__ = ['postcode']