[英]Where does the poll.id come from?
I'm studying the DjangoTutorial03 , and don't quite understand where the id
from poll.id
comes from. 我正在研究DjangoTutorial03 ,不太了解
poll.id
的id
来源。
poll.question
I get it, it's one of the attributes of class Poll
in models.py
. poll.question
我明白了,它是models.py
Poll
类的属性之一。 But not so much for poll.id
. 但是对于
poll.id
就不那么多了。 Is it an attribute inherited from models.Model
? 它是从
models.Model
继承的属性吗? I'm not sure because I searched for self.id
in both base.py
& __init__.py
from /django/db/models/
and nothing showed up. 我不确定,因为我在
/django/db/models/
base.py
和__init__.py
中都搜索了self.id
(is it the right place to search for?) (这是寻找合适的地方吗?)
The id
field is automatically created for your model if you don't provide any other field with primary_key=True
keyword argument. 如果您没有为其他字段提供
primary_key=True
关键字参数,则会为您的模型自动创建id
字段。 A good practice when dealing with primary keys is to use pk
attribute, as opposed to id
, because pk
is an alias to the real primary key field be it id
, my_id
, or anything else - poll.id
and poll.pk
are both the same. 处理主键的一个好习惯是使用
pk
属性,而不是id
,因为pk
是真实主键字段的别名,无论它是id
, my_id
还是其他任何东西poll.id
和poll.pk
都是相同。
The behaviour is caused by the Options
class ( here ) which is what you get when you access _meta
attribute, and it's attributes are derived from Model.Meta
. 该行为是由
Options
类( 在此处 )引起的,这是您访问_meta
属性时所得到的,并且它的属性派生自Model.Meta
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.