Why do I keep getting this "name 'Model' is not defined" error in my Django project?

Wadzanai Mufunde picture Wadzanai Mufunde · Jan 28, 2016 · Viewed 30k times · Source

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.

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.

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

Answer

masnun picture masnun · Jan 28, 2016

Fixing the issue with 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.

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.