Django:当没有定义\uuu init \uuu()时,模型类如何接受构造函数参数?

2024-04-24 04:29:12 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的应用程序中,我们有这个文件型号.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.pyQuestion的类定义中,没有任何构造函数参数的规范。那么这是怎么工作的呢?你知道吗


Tags: textfrompyimportdatemodelmodelsshell