Django poll教程出错

2024-04-23 23:43:33 发布

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

我刚从Django开始,我正在关注https://docs.djangoproject.com/en/1.10/intro/tutorial02/。你知道吗

当我运行时,shell接收到错误Question.objects.all全部()在壳中。我也运行dir(问题),它说它不存在,但我知道它存在。在我编辑之后重新整合了表/数据库型号.py几次都没有发现任何变化。我还添加了unicode方法,但没有解决这个问题谢谢。谢谢提前谢谢你的帮助。 这是我的型号.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 __unicode__(self):
        return self.Question

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

Tags: textfrompyimportselfmodelmodelsunicode
2条回答

对于初学者,可以更改__unicode__方法

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(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=8)

    def __unicode__(self):
        return self.choice_text

啊哈…我知道我现在做了什么。在我写问题之前,我用问题文本和选择文本进行了测试。它适用于问题文本和选择文本。打字的问题在于我忽略了一些简单的事情。 谢谢大家。你知道吗

相关问题 更多 >