Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django validators TypeError int()参数必须是字符串、类似字节的对象或数字,而不是';延迟属性_Django_Django Models_Django Validation - Fatal编程技术网

Django validators TypeError int()参数必须是字符串、类似字节的对象或数字,而不是';延迟属性

Django validators TypeError int()参数必须是字符串、类似字节的对象或数字,而不是';延迟属性,django,django-models,django-validation,Django,Django Models,Django Validation,我有这样的情况:当一个modelf字段被填充时,它不能大于其他模型的字段。我写了这个验证器 class Insumo(models.Model): nombre = models.CharField(max_length=100) cantidadexistencias = models.DecimalField(max_digits=50, decimal_places=3, validators=[MinValueValidator(0)]) def __str__

我有这样的情况:当一个modelf字段被填充时,它不能大于其他模型的字段。我写了这个验证器

class Insumo(models.Model):
    nombre = models.CharField(max_length=100)
    cantidadexistencias = models.DecimalField(max_digits=50, decimal_places=3, validators=[MinValueValidator(0)])

    def __str__(self):
        return self.nombre + " "+str(self.cantidadexistencias)



class InsumoProduction(models.Model):
    idproduction = models.ForeignKey('Production', on_delete=models.CASCADE)
    idinsumo = models.ForeignKey('Insumo', on_delete=models.CASCADE)
    #Validar que la cantidad usada sea maximo el numero de existencias
    cantidadusada = models.DecimalField(max_digits=50, decimal_places=3,validators=[insumo_existencias])



    def save(self,*args,**kwargs):
        insumo = Insumo.objects.get(id = self.idinsumo_id)
        insumo.cantidadexistencias -= self.cantidadusada
        insumo.save()
        super(InsumoProduction,self).save(*args,**kwargs)




    class Meta:
        constraints = [
            models.UniqueConstraint(fields=["idproduction", "idinsumo"],
                                    name='unique_insumoproduction')
        ]


    def __str__(self):
        return self.idproduction.idfinalproduct.idproduct.nombre +" "+ self.idproduction.idfinalproduct.idtaste.nombre  \
               + " "+ self.idinsumo.nombre + " "+ str(self.cantidadusada) + " Fecha Produccion " + str(
            self.idproduction.fechaproduccion)
这些是我的模型,我的验证器在这里:

from django.core.exceptions import ValidationError
from ubindustries.ubapi.models import models
from django.utils.translation import gettext_lazy as _


def insumo_existencias(value):
    insumo = models.Insumo.objects.get(id=models.InsumoProduction.idinsumo_id)

    if (insumo.cantidadexistencias < value):
        raise ValidationError(
            _('Error no hay existencias suficientes'),
        )
来自django.core.exceptions的
导入验证错误
从ubindustries.ubapi.models导入模型
从django.utils.translation导入gettext\u lazy作为_
def insumo_存在(值):
insumo=models.insumo.objects.get(id=models.InsumoProduction.idisumo\u id)
如果(insumo.CANTIDADESTENCIAS<值):
引发验证错误(
_(‘错误不存在’),
)
我收到了这个错误消息:TypeError位于/ubapi/insumoproduction/add/
int()参数必须是字符串、类似对象的字节或数字,而不是'DeferredAttribute

我不确定您认为
models.inumoproduction.idisumo_id
在做什么,但它肯定是错的。不能将值与字段定义进行比较。应该如何比较?不应该。您需要解释您试图对该行执行的操作。尝试获取与insumoproduction表中的id关联的insumo对象