2014-10-16 63 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。