Usman Gill
Usman Gill

Reputation: 31

Django CharField not defined

from django.db import models

class Pet(models.Model):
    SEX_CHOICES = [('M','Male'), ('F', 'Female')]
    name = CharField(max_length=100)
    submitter = CharField(max_length=100)
    species = CharField(max_length=30)
    breed = CharField(max_length=30, blank=True)
    description = models.TextField()
    sex = models.CharField(choices=SEX_CHOICES, max_length=1, blank=True)
    submission_date = models.DateTimeField()
    age = models.IntegerField(null=True)
    vaccinations = models.ManyToManyField('Vaccine', blank=True)


class Vaccine(models.Model):
    name = models.CharField(max_length=50)

With this code I am getting command line error when running "python manage.py makemigrations". name "CharField" is not defined.

How to fix it.

Upvotes: 1

Views: 4821

Answers (3)

JPG
JPG

Reputation: 88489

You should add CharField class to module imports, ie,

from django.db import models
from django.db.models import CharField



But, this isn't a right way to import modules in your projects. So change your models.py as follows,

from django.db import models

class Pet(models.Model):
    SEX_CHOICES = [('M','Male'), ('F', 'Female')]
    name = models.CharField(max_length=100)
    submitter = models.CharField(max_length=100)
    species = models.CharField(max_length=30)
    breed = models.CharField(max_length=30, blank=True)
    description = models.TextField()
    sex = models.CharField(choices=SEX_CHOICES, max_length=1, blank=True)
    submission_date = models.DateTimeField()
    age = models.IntegerField(null=True)
    vaccinations = models.ManyToManyField('Vaccine', blank=True)


class Vaccine(models.Model):
    name = models.CharField(max_length=50)

Upvotes: 6

Bigga
Bigga

Reputation: 598

Shouldn't it be like this :

from django.db import models

class Pet(models.Model):
    SEX_CHOICES = [('M','Male'), ('F', 'Female')]
    name = models.CharField(max_length=100)
    submitter = models.CharField(max_length=100)
    species = models.CharField(max_length=30)
    breed = models.CharField(max_length=30, blank=True)
    description = models.TextField()
    sex = models.CharField(choices=SEX_CHOICES, max_length=1, blank=True)
    submission_date = models.DateTimeField()
    age = models.IntegerField(null=True)
    vaccinations = models.ManyToManyField('Vaccine', blank=True)


class Vaccine(models.Model):
    name = models.CharField(max_length=50)

Upvotes: 0

Omar Einea
Omar Einea

Reputation: 2524

You used CharField directly instead of models.CharField in some of your declarations.

This should do:

from django.db import models

class Pet(models.Model):
    SEX_CHOICES = [('M','Male'), ('F', 'Female')]
    name = models.CharField(max_length=100)
    submitter = models.CharField(max_length=100)
    species = models.CharField(max_length=30)
    breed = models.CharField(max_length=30, blank=True)
    description = models.TextField()
    sex = models.CharField(choices=SEX_CHOICES, max_length=1, blank=True)
    submission_date = models.DateTimeField()
    age = models.IntegerField(null=True)
    vaccinations = models.ManyToManyField('Vaccine', blank=True)


class Vaccine(models.Model):
    name = models.CharField(max_length=50)

Upvotes: 1

Related Questions