'question_text'是此函数的无效关键字参数



我正在尝试按照民意调查教程进行操作,我包括来自项目的urls.py和民意调查目录中的models.py

q = Question(question_text="some text", pub_date=timezone.now))

导致以下错误:

'question_text' is  an invalid keyword argument for this function.

我的网站/网址.py

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]

民意调查/模型.py

import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text

如果你在 django shell 中执行此操作,你首先需要导入你正在使用的模型,在你的例子中你需要导入模型问题

让我们从头开始回忆一切。

第 1 步:激活外壳

python manage.py shell

第 2 步:导入模型问题并导入时区

from polls.models import Question
from django.utils import timezone

步骤 3:现在运行查询

q = Question(question_text="What's new?", pub_date=timezone.now())

如果你注意到了,根据你的问题,这就是你做错了什么。

步骤 4:运行保存方法

q.save()

所有这些都是w.r.t民意调查教程使用api。

我认为在学习教程时,您已经做了很多代码更改,而这些更改并未反映在SQL数据库中。

因此,在执行上述步骤之前,请按照波纹管步骤进行操作。

python manage.py migrate
python manage.py makemigrations polls
python manage.py migrate

现在这样做

python manage.py shell 
from polls.models import Question 
from django.utils import timezone 
q = Question(question_text="What's new?", pub_date=timezone.now() 
q.save()

最新更新