Django教程第2步
我在django教程的shell部分,我在polls/models.py中添加了________;方法 这是我的models.py:Django教程第2步,django,database,models,Django,Database,Models,我在django教程的shell部分,我在polls/models.py中添加了________;方法 这是我的models.py: from __future__ import unicode_literals from django.db import models # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=200)
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
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.question_text
class Question(models.Model):
# ...
def was_published_recently(self):
但是,当我运行服务器时,出现以下错误:
ERRORS:
polls.Choice.question: (fields.E300) Field defines a relation with model 'Question', which is either not installed, or is abstract.
有人能告诉我在哪里把模型搞错了吗。py我找不到该教程的完整示例。你不应该有两个名为问题的模型类删除第二个并将函数移到第一个问题类
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):
##
您已经创建了两个名为问题的模型。因此,请删除第二个。它会产生冲突,这就是为什么Choice类找不到问题模型的原因。为什么要将类
问题定义两次?哦,那么我应该把第二个定义放在那里?我会试试看,那个教程有点难理解