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
精彩评论