Django Model() 与 Model.objects.create() [英] Django Model() vs Model.objects.create()

查看:28
本文介绍了Django Model() 与 Model.objects.create()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行两个命令有什么区别:

What it the difference between running two commands:

foo = FooModel()

bar = BarModel.objects.create()

第二个是否立即在数据库中创建一个BarModel,而对于FooModel,必须显式调用save()方法将其添加到数据库中?

Does the second one immediately create a BarModel in the database, while for FooModel, the save() method has to be called explicitly to add it to the database?

推荐答案

https://docs.djangoproject.com/en/stable/topics/db/queries/#creating-objects

要在单个步骤中创建和保存对象,请使用 create() 方法.

To create and save an object in a single step, use the create() method.

这篇关于Django Model() 与 Model.objects.create()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆