首页 > 关于python获取对象?

关于python获取对象?

如果我查询user表中的数据,按照id查询。请问下面这俩种方法有什么区别吗?
User(id=2)
User.objects.get(id=2)
非常感谢!


这个好像偷换了不同的概念了。

User(id=2),是创建一个User实例, 然后User中的id=2。这个需要save()才能把数据存放到数据库里。
User.objects.get(id=2),才是去查找User表里的数据。这个返回是数据库的数据。

Django Office docs:

A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.
By default, Django adds a Manager with the name objects to every Django model class

【热门文章】
【热门文章】