Python 为什么我在我的 Django 项目中不断收到这个“名称'模型'未定义”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35071234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I keep getting this "name 'Model' is not defined" error in my Django project?
提问by Wadzanai Mufunde
I've looked through the stackoverflow questions under this topic already and I moved my Trainer class to be above my Class_Training class, but I still keep getting the same "name 'Model' is not defined" error when I enter 'manage.py create superuser' on my command prompt.
我已经查看了该主题下的 stackoverflow 问题,并且将 Trainer 类移到了 Class_Training 类的上方,但是当我输入“manage.py create”时,我仍然收到相同的“名称 'Model' 未定义”错误超级用户”在我的命令提示符下。
Also, I am having diffuclty migrating my models. I tried 'django-admin makemigrations training' but django-admin as not recoginised; and 'manage.py makemigrations training' but makemigrations was not recognised.
另外,我很难迁移我的模型。我尝试过“django-admin makemigrations training”,但无法识别 django-admin;和 'manage.py makemigrations training' 但 makemigrations 未被识别。
How do I migrate my models?
如何迁移我的模型?
Here is my code:
这是我的代码:
#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
采纳答案by masnun
Fixing the issue with Model
解决问题 Model
You have used models, Model
instead of models.Model
in some of your model definitions. The Model
class is in the model
module. That's why we use a .
instead of a comma.
您在某些模型定义中使用了models, Model
而不是models.Model
。该Model
班是在model
模块。这就是为什么我们使用 a.
而不是逗号的原因。
Room model:
房间模型:
class Room(models.Model):
Hero model:
英雄模型:
class Hero(models.Model):
Trainer model:
教练机型号:
class Trainer(models.Model):
And finally:
最后:
class Class_Training(models.Model):
Fixing the issue with Migrations
解决迁移问题
It should be:
它应该是:
python manage.py makemigrations
You need python
command. Also check if you are in the directory where your manage.py
is.
你需要python
命令。还要检查您是否在您所在的目录中manage.py
。