简体   繁体   中英

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.

Calling People.objects.get(pk=code) (calling get ) will result in the People object with pk=code returned, or an error if not found.

It's a query to get the People object that has a primary key of whatever the value of "code" is.

By default, all Django model instances have a primary key that uniquely identifies the object. 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

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(). It doesn't make any sense to give a pk to all() since it just returns all the objects of that type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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