简体   繁体   English

poll.id来自哪里?

[英]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.idid来源。

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是真实主键字段的别名,无论它是idmy_id还是其他任何东西poll.idpoll.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.

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