Django only adding one field from model

时间:2016-02-12 21:50:24

标签: python django django-models django-migrations

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 Reports extends React.Component {
    render() {
        return (
                <div>
                    <h2>Reports</h2>
                </div>
        )
    }
}

module.exports = Reports;

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

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)

2 个答案:

答案 0 :(得分:2)

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

operations = [
    migrations.AddField(
        model_name='testimonial',
        name='test',
        field=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)

答案 1 :(得分:0)

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

Ping ipping = new Ping();
PingReply replyab01 = ipping.Send(abilene01, 1000);
if (replyab01 != null)
{
    (if replyab01.Status == IPStatus.Success)
    {
        ZplPrinterStatus.Items.Add("Abilene Primary(01) Printer Status:" + replyab01.Status);
    }
}
else
{
    ListItem item = new ListItem("Abilene Primary(01) Printer Status:" + "Error Printer Time out");
    item.Attributes["style"] = "color:red;";
    ZplPrinterStatus.Items.Add(item);
}

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