Django执行makemigrations命令报错:Passenger模型参数异常
Django makemigrations 报错:TypeError: init() got an unexpected keyword argument 'max_lenght'
正在学习CS50课程,此前Airport与Flights模型可正常运行,新增Passenger模型后执行python manage.py makemigrations命令失败,报错提示TypeError: __init__() got an unexpected keyword argument 'max_lenght'。
我的models.py代码
from django.db import models class Airport(models.Model): city=models.CharField(max_length=64) code=models.CharField(max_length=3) def __str__(self): return f"{self.city} ({self.code})" # Create your models here. class Flights(models.Model): origin=models.ForeignKey(Airport , on_delete= models.CASCADE , related_name="departures") destination=models.ForeignKey(Airport , on_delete=models.CASCADE , related_name="arrivals") duration=models.IntegerField() def __str__(self): return f" {self.origin} to {self.destination} : {self.duration}" class Passenger(models.Model): first=models.CharField(max_length=24) last=models.CharField(max_lenght=24) flight=models.ManyToManyField(Flights , blank=True , related_name="passengers") def __str__(self): return f"{self.first} {self.last}"
完整报错堆栈
$ python manage.py makemigrations Traceback (most recent call last): File "D:\practice\airlines\manage.py", line 22, in <module> main() File "D:\practice\airlines\manage.py", line 18, in main execute_from_command_line(sys.argv) File "C:\Users\faisa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line utility.execute() File "C:\Users\faisa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 416, in execute django.setup() File "C:\Users\faisa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "C:\Users\faisa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 116, in populate app_config.import_models() File "C:\Users\faisa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 269, in import_models self.models_module = import_module(models_module_name) File "C:\Users\faisa\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "D:\practice\airlines\flights\models.py", line 19, in <module> class Passenger(models.Model): File "D:\practice\airlines\flights\models.py", line 21, in Passenger last=models.CharField(max_lenght=24) File "C:\Users\faisa\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1139, in __init__ super().__init__(*args, **kwargs) TypeError: __init__() got an unexpected keyword argument 'max_lenght'
问题解决
报错原因是拼写错误:Passenger模型中last=models.CharField(max_lenght=24)里的参数名max_lenght拼写错误,正确写法应为max_length(注意是length而非lenght)。
修正后的Passenger模型代码:
class Passenger(models.Model): first=models.CharField(max_length=24) last=models.CharField(max_length=24) flight=models.ManyToManyField(Flights , blank=True , related_name="passengers") def __str__(self): return f"{self.first} {self.last}"
修正后重新执行python manage.py makemigrations和python manage.py migrate命令即可完成数据库迁移。
内容的提问来源于stack exchange,提问作者Mohammed Faisal Khan
相关产品推荐
相关产品推荐

