gpt4 book ai didi

python - 为什么我的 Django 项目中不断出现“"name ' 模型未定义”错误?

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:52 24 4
gpt4 key购买 nike

我已经查看了该主题下的 stackoverflow 问题,并将我的 Trainer 类移动到我的 Class_Training 类之上,但是当我输入“管理”时,我仍然不断收到相同的“名称‘模型’未定义”错误.py 在我的命令提示符下创建 super 用户。

此外,我在迁移模型时遇到了困难。我试过 'django-admin makemigrations training' 但 django-admin 没有被识别;和 'manage.py makemigrations training' 但 makemigrations 未被识别。

如何迁移我的模型?

这是我的代码:

    #from django.db import models
from django_pg import models

# Create your models here.
TRAINING_TYPE_CHOICES = (
('AC', 'Armed Combat'),
('UC', 'Unarmed Combat'),
('P', 'Piloting'),
('O', 'Other'),
)

GENDER_CHOICES = (
('F', 'Female'),
('M', 'Male'),
('U', 'Unspecified'),
)
OUTCOME_CHOICES = (
('P', 'Pass'),
('F', 'Fail'),
)

class Trainer(models, Model):
first_name = models.CharField(max_length = 25)
surname = models.CharField(max_length = 30)
address = models.CharField(max_length = 200)
gender = models.CharField(max_length = 1, choices = GENDER_CHOICES)
citizenship = models.CharField(max_length = 30)
email = models.EmailField(max_length = 30)

class Class_Training(models, Model):
trainer = models.ForeignKey('Trainer')
class_name = models.CharField(max_length = 30)
type_of_class = models.CharField(max_length = 2, choices= TRAINING_TYPE_CHOICES)
description = models.TextField(max_length = 200)

def __str__(self):
return self.class_name, self.trainer


class ReportLog(models.CompositeField):
class_ID = models.IntegerField
hero_ID = models.IntegerField
outcome = models.CharField(max_length = 1, choices = OUTCOME_CHOICES)
comments = models.TextField
trainer = models.IntegerField

class Meta:
db_type = 'report'

class Attendance(models.CompositeField):
class_ID = models.IntegerField
hero_ID = models.IntegerField
room_name = models.CharField(max_length = 30)
date = models.DateField
start_time = models.TimeField
end_time = models.TimeField

class Meta:
db_type = 'attendance'

class Room(models, Model):
room_name = models.CharField(max_length = 20)

class Hero(models, Model):
codename = models.CharField(max_length = 20)

def __str__(self):
return self.codename

最佳答案

修复模型的问题

您在某些模型定义中使用了 models, Model 而不是 models.ModelModel 类位于 model 模块中。这就是我们使用 . 而不是逗号的原因。

房间模型:

class Room(models.Model):

英雄模型:

class Hero(models.Model):

训练模型:

class Trainer(models.Model):

最后:

class Class_Training(models.Model):

解决迁移问题

应该是:

python manage.py makemigrations

你需要 python 命令。还要检查您是否位于 manage.py 所在的目录中。

关于python - 为什么我的 Django 项目中不断出现“"name ' 模型未定义”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35071234/

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