TJB
TJB

Reputation: 3846

Django only adding one field from model

When I edit a model to have more fields, and make migrations, Django won't add a new field without removing the old one.

Here is the model

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)

This is what I get in the terminal

Migrations for 'testimonials':
0004_auto_20160212_1537.py:
- Remove field quote from testimonial
- Add field test to testimonial

and this is the most recent migration

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),
        ),
    ]

Upvotes: 3

Views: 908

Answers (2)

Sayse
Sayse

Reputation: 43300

I'm pretty sure the issue is caused by the commas you have for the trailing fields, in python, this is indicating that you're creating a tuple and it will treat the next line as a continuation of that tuple object, you need to remove them

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)

should be

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)

Upvotes: 4

Denis Sukhoverkhov
Denis Sukhoverkhov

Reputation: 90

If you do not want to remove the "quote" - simply remove manually the operation of this migration.

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

And execute: python manage.py migrate app_name. Perhaps when you create a migration, you accidentally commented out this field.

Upvotes: 0

Related Questions