Django教程第2步

时间:2016-06-25 22:31:50

标签: django database models

我在django教程的shell部分我将____str____方法添加到民意调查/ 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)
    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.

有人可以展示我的模型。我无法找到该教程的完整示例。

2 个答案:

答案 0 :(得分:1)

您不应该有两个名为问题的模型类删除第二个模型类并将该函数移动到第一个问题类中。

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):
        ##

答案 1 :(得分:0)

您已创建2个名称为问题的模型。所以请删除第二个。它会造成碰撞,这就是为什么Choice类找不到问题模型。