Django CharField没有定义

时间:2018-03-04 08:03:00

标签: python django django-models

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)

使用此代码,我在运行" python manage.py makemigrations"时遇到命令行错误。 名字" CharField"没有定义。

如何修复它。

2 个答案:

答案 0 :(得分:1)

您在某些声明中直接使用了CharField而不是models.CharField

这应该做:

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)

答案 1 :(得分:0)

不应该是这样的:

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)