python - Django 只从模型中添加一个字段

标签 python django django-models django-migrations

当我编辑模型以包含更多字段并进行迁移时,Django 不会在不删除旧字段的情况下添加新字段。

这是模型

class Testimonial(models.Model):
    name = models.CharField(max_length=20, null=True),
    quote = models.CharField(max_length=255, null=True),
    test = models.CharField(max_length=20, null=True)

这是我在终端中得到的

“推荐”的迁移:
0004_auto_20160212_1537.py:
- 从推荐中删除字段引用
- 将现场测试添加到推​​荐

这是最近的迁移

from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('testimonials', '0003_auto_20160212_1536'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='testimonial',
            name='quote',
        ),
        migrations.AddField(
            model_name='testimonial',
            name='test',
            field=models.CharField(max_length=20, null=True),
        ),
    ]

最佳答案

我很确定问题是由尾随字段的逗号引起的,在 python 中,这表明您正在创建一个元组,它将把下一行视为该元组对象的延续,你需要删除它们

class Testimonial(models.Model):
    name = models.CharField(max_length=20, null=True),
    quote = models.CharField(max_length=255, null=True),
    test = models.CharField(max_length=20, null=True)

应该是

class Testimonial(models.Model):
    name = models.CharField(max_length=20, null=True)
    quote = models.CharField(max_length=255, null=True)
    test = models.CharField(max_length=20, null=True)

关于python - Django 只从模型中添加一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35372778/

相关文章:

django - 名称错误 : name 'TypeError' is not defined in Apache logs

python - 如何将字符串列表打印为没有中间字符的单个字符串

Django-rest-auth (dj-rest-auth) 自定义用户注册

python - 如何从网站抓取 2 级网页

python - Django-Notification - 电子邮件后端的替代方案

python - 检查变量是否在 django 模板语言中定义

python - Django - JSON 序列化错误

django - 只有一个 django 模型实例

python - 如何防止 BeautifulSoup 自动关闭看起来像标签但实际上不是的东西?

python - astropy.table.write() IOError : File exists: