创建Django模型后执行migrate报错,请求技术排查解决
问题排查与解决
核心错误原因
你的代码中存在拼写错误:模型类需要继承的是models.Model(大写字母M),而非models.model(小写字母m),这是触发TypeError: function() argument 'code' must be code, not str错误的直接原因。
修正后的Post模型代码
from django.db import models from django.utils import timezone from django.contrib.auth.models import User # Create your models here. class Post(models.Model): # 此处将model改为Model author = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=43) image = models.ImageField(null=True) content = models.TextField() date_posted = models.DateTimeField(default=timezone.now)
后续操作提示
修正代码后,依次执行以下命令完成数据库迁移:
python manage.py makemigrationspython manage.py migrate
你此前在admin.py注册模型、在settings.py的INSTALLED_APPS中添加blog1应用的操作是正确的,无需修改。
内容的提问来源于stack exchange,提问作者rick de graaf
相关产品推荐
相关产品推荐

