简体   繁体   English

Django主键

[英]Django primary key

在django中查询时,说People.objects.all(pk=code)pk=code是什么意思?

Calling People.objects.all(pk=code) (calling all ) will result in the pk=code being ignored and a QuerySet for all People returned. 调用People.objects.all(pk=code) (调用all )将导致pk = code被忽略,并返回所有People的QuerySet。

Calling People.objects.get(pk=code) (calling get ) will result in the People object with pk=code returned, or an error if not found. 调用People.objects.get(pk=code) (调用get )将导致返回pk = code的People对象,如果找不到则返回错误。

It's a query to get the People object that has a primary key of whatever the value of "code" is. 这是一个查询,以获取具有主键的People对象,无论“ code”的值是多少。

By default, all Django model instances have a primary key that uniquely identifies the object. 默认情况下,所有Django模型实例都有一个唯一标识对象的主键。 Generally it's an auto-incrementing integer, but you could define it to be whatever you want, so long as it's certain to be unique. 通常,它是一个自动递增的整数,但是您可以将其定义为任意值,只要它可以确定是唯一的即可。

http://docs.djangoproject.com/en/dev/topics/db/models/#id1 http://docs.djangoproject.com/en/dev/topics/db/models/#id1

Edit: Now that I look at the code snippet a little closer, rather than just assuming what it said, it doesn't make much sense. 编辑:现在,我更加仔细地查看了代码片段,而不只是假设它说了什么,这没有多大意义。 The all() method should be a get(). all()方法应为get()。 It doesn't make any sense to give a pk to all() since it just returns all the objects of that type. 给pk给all()没有任何意义,因为它只返回该类型的所有对象。

http://docs.djangoproject.com/en/dev/ref/models/querysets/#all http://docs.djangoproject.com/en/dev/ref/models/querysets/#id5 http://docs.djangoproject.com/en/dev/ref/models/querysets/#all http://docs.djangoproject.com/en/dev/ref/models/querysets/#id5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM