开发者

Django primary key

When querying in django say People.objects.all(pk=code), what does pk=code开发者_如何学运维 mean?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜