更新我的mod中的新字段

2024-06-28 15:11:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更新数据库中有关已具有上述字段的“卡”模型的新字段,但有一个问题阻碍了我执行此过程:

当我运行./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

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

型号.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(上传到=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()

如果我忽略了什么,请提前向我道歉。

谢谢!!


Tags: topyselfadddefaultfieldthatmanage