gpt4 book ai didi

python - 更新我的模型中的新字段

转载 作者:太空狗 更新时间:2023-10-30 03:01:10 26 4
gpt4 key购买 nike

我正在尝试更新我的数据库中关于我的“卡片”模型的新字段,该模型已经有上面的字段,但是我遇到了一个阻碍我执行此过程的问题:

当我运行 ./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(upload_to=url, default='')

但是在对“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()

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

谢谢!!

最佳答案

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)

关于python - 更新我的模型中的新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26413153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com