在我的应用程序中,我们有这个文件型号.py地址:
from __future__ import unicode_literals
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
在用python manage.py shell
启动shell之后,我们可以这样写:
from polls.models import Question
q = Question(question_text = "What's new?", pub_date=timezone.now())
在创建对象时,Question
类显然接受构造函数参数。但是在models.py
中Question
的类定义中,没有任何构造函数参数的规范。那么这是怎么工作的呢?你知道吗
因为
Question
类继承了models.Model
类,所以它自己从那里获取构造函数。你知道吗相关问题 更多 >
编程相关推荐