简体   繁体   English

为什么我的Django项目中总是出现此“名称未定义模型”错误?

[英]Why do I keep getting this “name 'Model' is not defined” error in my Django project?

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”时,仍然出现相同的“未定义模型名称”错误超级用户”。

Also, I am having diffuclty migrating my models. 另外,我在迁移模型方面有些困难。 I tried 'django-admin makemigrations training' but django-admin as not recoginised; 我尝试过“ django-admin makemigrations培训”,但是django-admin没有重新识别。 and 'manage.py makemigrations training' but makemigrations was not recognised. 和“ manage.py makemigrations培训”,但未识别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

Fixing the issue with Model 解决Model问题

You have used models, Model instead of models.Model in some of your model definitions. 您已在某些模型定义中使用models, Model而不是models.Model The Model class is in the model module. Model类位于model模块中。 That's why we use a . 这就是为什么我们使用. instead of a comma. 而不是逗号。

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. 您需要python命令。 Also check if you are in the directory where your manage.py is. 还要检查您是否位于manage.py所在的目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不断收到名称错误:名称“模型”未定义 - Why do I keep getting Name Error: name 'model' is not defined 为什么我不断收到名称未定义的错误? - Why do I keep getting the name not defined error? 为什么我的 Django/Bootstrap 项目中不断出现 multiValueDictKey 错误? - Why do I keep getting a multiValueDictKey Error in my Django/Bootstrap project? 我不断收到错误消息,说我的类名未在 python 中定义 - i keep getting the error saying that my class name is not defined in python 所以我在学校项目的 Tkinter 代码中不断收到这个名称错误,我不太明白为什么 - so i keep getting this name error in my Tkinter code for a school project and i dont quite understand why 为什么我一直收到此 python 错误 cost is not defined? - why do i keep getting this python error cost is not defined? 为什么我在 Django 3.2 中不断收到 NoReverse 错误 - Why do I keep getting a NoReverse Error in Django 3.2 为什么在上述类中定义TwitterClient时,为什么不断收到93行错误,提示未定义TwitterClient? - Why do I keep getting a line 93 error saying TwitterClient not defined when it is defined in a class above? 为什么我不断收到NameError:未定义名称'PS' - Why I keep getting NameError: name 'PS' is not defined 我不断收到错误名称'y_test'未定义 - I keep on getting the error name 'y_test' is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM