Django CharField没有定义

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django CharField没有定义相关的知识,希望对你有一定的参考价值。

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”未定义。

如何解决它。

答案

您应该将CharField类添加到模块导入,即,

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

但是,这不是在项目中导入模块的正确方法。所以改变你的models.py如下,

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)
另一答案

您在某些声明中直接使用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)
另一答案

不应该是这样的:

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)

以上是关于Django CharField没有定义的主要内容,如果未能解决你的问题,请参考以下文章

django—Form组件

如何使用django自定义表单保存多个到多个字段

没有最大长度的 Django CharField

Django - 自定义Form校验规则

Django REST framework序列化

Django:仅使用 annotate() 和 values() 计算非空 CharField