gpt4 book ai didi

Django 教程第 2 步

转载 作者:搜寻专家 更新时间:2023-10-30 23:38:39 24 4
gpt4 key购买 nike

我在 django 教程的 shell 部分我将 ____str____ 方法添加到 polls/models.py

这是我的模型.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.

谁能告诉我我在哪里弄错了我的 models.py 我找不到该教程的完整示例。

最佳答案

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

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

关于Django 教程第 2 步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com