更新模型中的新字段

时间:2014-10-16 20:18:22

标签: python django field django-models

我试图在我的数据库中更新关于我的" card"的新字段。已有上述字段的模型,但我遇到了阻碍我执行此过程的问题:

当我运行./manage.py syncdb时,我收到了这条消息:

Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

所以我运行了makemigrations命令,但是......

You are trying to add a non-nullable field 'imagen' to card without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py

我选择按第二个选项并自己添加要求,实际上我有这个:

models.py:

from django.db import models

class subscriber(models.Model):
    nombre = models.CharField(max_length=200)
    apellidos = models.CharField(max_length=200)
    status = models.BooleanField(default=True)

    def __unicode__(self):
        nombreCompleto = "%s %s"%(self.nombre,self.apellidos)
        return nombreCompleto

def url(self,filename):
    ruta = "MultimediaData/Card/%s/%s"%(self.nombre,str(filename))
    return ruta

class card(models.Model):

    nombre = models.CharField(max_length=100)
    descripcion = models.TextField(max_length=300)
    status = models.BooleanField(default=True)
    imagen = models.ImageField(upload_to=url)
    precio = models.DecimalField(max_digits=6,decimal_places=2)
    stock = models.IntegerField()

    def __unicode__(self):
        return self.nombre

如果我修改" Imagen"字段就像我说的那样消息如下:

imagen = models.ImageField(upload_to = url,默认='')

但是,对" imagen"进行相同的修改后,会出现相同的消息。字段:

You are trying to add a non-nullable field 'precio' to card without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:

最后是最后一次:

You are trying to add a non-nullable field 'stock' to card without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:

如果我修改了所有这些字段,我终于可以运行./manage.py makemigrations:

Migrations for 'synopticup':
  0002_auto_20141016_2004.py:
    - Add field imagen to card
    - Add field precio to card
    - Add field stock to card

但是当我运行./manage.py syncdb时,我得到了这个错误:

django.core.exceptions.ValidationError: [u"'' value must be a decimal number."]

我的流程出了什么问题?我更喜欢以前所有的方式离开:

class card(models.Model):

    nombre = models.CharField(max_length=100)
    descripcion = models.TextField(max_length=300)
    status = models.BooleanField(default=True)
    imagen = models.ImageField(upload_to=url)
    precio = models.DecimalField(max_digits=6,decimal_places=2)
    stock = models.IntegerField()

提前道歉我的广泛问题,如果我忽略了什么。

谢谢!

2 个答案:

答案 0 :(得分:4)

DecimalField的默认值应为Decimal个对象。

from decimal import Decimal

class card(models.Model):
    # ...
    imagen = models.ImageField(upload_to=url, default='')
    precio = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal(0))
    stock = models.IntegerField(default=0)

答案 1 :(得分:0)

在向model.fixed添加新字段时遇到了同样的问题,删除了django_migrations表中的迁移记录以及项目清除下的迁移文件。