I'm studying the DjangoTutorial03 , and don't quite understand where the id
from poll.id
comes from.
poll.question
I get it, it's one of the attributes of class Poll
in models.py
. But not so much for poll.id
. Is it an attribute inherited from 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. (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. 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.
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
.
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.