Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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
Json 使用外部值序列化Django queryset_Json_Django_Django Queryset_Django Serializer - Fatal编程技术网

Json 使用外部值序列化Django queryset

Json 使用外部值序列化Django queryset,json,django,django-queryset,django-serializer,Json,Django,Django Queryset,Django Serializer,我正在使用Django 1.7,我有这个模型 class Producto(models.Model): item = models.CharField(max_length=10) descripcion = models.CharField(max_length=140) unidad = models.CharField(max_length=5) usuario = models.ForeignKey(User) alta = models

我正在使用Django 1.7,我有这个模型

class Producto(models.Model):
    item = models.CharField(max_length=10)
    descripcion = models.CharField(max_length=140)
    unidad = models.CharField(max_length=5)    
    usuario = models.ForeignKey(User)
    alta = models.DateTimeField(auto_now_add=True)
    imagen = models.ImageField(upload_to='images/producto/',blank=True,null=True)
    def __unicode__(self):
        return self.item

class OEntrada(models.Model):
    folio = models.CharField(max_length=15)
    codigo_proveedor = models.CharField(max_length=15)
    nombre_proveedor = models.CharField(max_length=100)
    status = models.IntegerField(default=0)
    fecha = models.DateTimeField(auto_now_add=True)
    def __unicode__(self):
        return self.folio

class OEntradaDetalle(models.Model):
    oentrada = models.ForeignKey(OEntrada,related_name='detalles')
    producto = models.ForeignKey(Producto)
    cantidad_ordenada = models.DecimalField(default=0,decimal_places=2, max_digits=10)
    cantidad_recibida = models.DecimalField(default=0,decimal_places=2, max_digits=10)
    fecha_entrega = models.DateField(blank=True,null=True)
    epc = models.CharField(max_length=25,null=True,blank=True)
Ang我想制作一个JSON,其中包含一些特定的数据和一些过滤器,只包含库存中没有的产品 我想从OEntradaDetalle获得oentrada.folio、producto.item、cantidad_recibida和epc

我试着注释:

def producto_para_ingreso(request,folio_orden_entrada):
    inventario = Inventario.objects.all().values('epc')
    pendientes = OEntradaDetalle.objects.filter(oentrada__folio=folio_orden_entrada,
        cantidad_recibida__gt=0).exclude(epc__in=inventario).annotate(item='producto__item')
    response = serializers.serialize('json',pendientes)
    return HttpResponse(response)
我还尝试了值:它以丑陋的格式返回我想要的值,比如:{producto_uuuuitem:u'ITEM1',cantidad_recibida:Decimal'100'}

实际上这就是我现在拥有的:JSON

{
        "fields": {
            "producto": 7,
            "oentrada": 1,
            "epc": "122C00000829",
            "fecha_entrega": null,
            "cantidad_ordenada": "1",
            "cantidad_recibida": "1"
        },
        "model": "inventario_rfid.oentradadetalle_deferred_cantidad_ordenada_fecha_entrega_oentrada_id",
        "pk": 3
    },
我需要这样的东西:

{
    "fields": {
        "producto": "ITEM-1", <<I need a Foreign Value, not the ID
        "oentrada": "E01",    << Same in this, I need a Foreign Value not the Id
        "epc": "122C00000829",
        "fecha_entrega": null,
        "cantidad_ordenada": "1",
        "cantidad_recibida": "1"
    },
    "model": "inventario_rfid.oentradadetalle_deferred_cantidad_ordenada_fecha_entrega_oentrada_id",
    "pk": 3
},
如果我能得到一个干净的JSON(不包括字段、模型和pk信息),那就太糟糕了


谢谢

您可以在serializers.py中执行此操作

class ProductoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Producto


class OEntradaDetalleSerializer(serializers.ModelSerializer):
    producto = ProductoSerializer(many=True)

    class Meta:
        model = OEntradaDetalle
        fields = ('producto', epc', ....)# write the field which you needed

我认为与您的要求类似,如果您使用的是restful,我建议使用Django Rest框架。我看到了,但我使用的是Django 1.7和Wadofsuff.Django.serializers.json不再工作,我也尝试了natural_键,但没有luckI我也使用Django Rest框架,但是,我怎样才能用REST来完成这件事?我试过类似的方法,但这里有另一个细节。。。我可以把3个过滤条件放在哪里?我需要在cantidad_recibida字段中超过0且epc未在库存过滤器entrada__folio=folio_orden_entrada,cantidad_recibida_gt=0中注册的一个oentrada_folio产品。excludeepc_in=inventario。这使得我选择了比API更手动的选项
class ProductoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Producto


class OEntradaDetalleSerializer(serializers.ModelSerializer):
    producto = ProductoSerializer(many=True)

    class Meta:
        model = OEntradaDetalle
        fields = ('producto', epc', ....)# write the field which you needed