2014-10-16 64 views
3

我想更新我的分贝新的领域有关已经有了上述领域我的“卡”模式,但我有妨碍我做这个过程中的一个问题:在我的模型更新的新领域

当我跑./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 =网址,默认= '' )

但随后的相同的消息出现在作出相同的修改为“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() 

apologizeme。

谢谢!

回答

3

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) 
+0

我有同样的问题,但与FileField,我应该保持默认? – tilaprimera 2014-11-10 05:36:53

+0

tilaprimera,使您的文件字段可为空,那么您不必担心有一个不存在的默认文件,然后将默认文件设置为空(或“') – 2017-02-06 17:04:57

0

遇到同样的问题时,新的字段添加到通过在项目驱气django_migrations表中删除迁移记录与迁移文件model.fixed。