执行makemigrations遇AttributeError:django.db.models无ManytoManyField
Django makemigrations 报错:AttributeError: module 'django.db.models' has no attribute 'ManytoManyField'
嘿,这个问题其实就是个容易忽略的拼写小失误!让我给你拆解一下:
你执行python manage.py makemigrations时触发了错误,核心报错信息是:
AttributeError: module 'django.db.models' has no attribute 'ManytoManyField'
从Traceback里能看到,问题出在你的adoptions/models.py第13行的代码:
vaccinations = models.ManytoManyField('Vaccine', blank=True)
错误原因
Django中定义多对多关系的字段类正确名称是ManyToManyField——注意中间的to部分,第二个字母M是大写的,而你写成了小写的t(ManytoManyField),这就导致Django找不到这个属性,所以抛出了AttributeError。
修复步骤
- 打开你的
C:\Users\Bryan\Desktop\wisdompets\adoptions\models.py文件 - 定位到第13行,把
ManytoManyField修改为正确的ManyToManyField - 保存文件后,重新执行
python manage.py makemigrations,就能正常生成迁移文件了
内容的提问来源于stack exchange,提问作者Nnemeka300
相关产品推荐
相关产品推荐

