0

我正在使用 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)

我想用一些过滤器制作一个带有一些特定数据的 JSON,只取不在库存中的产品(我有这个)我想得到oentrada.folio,producto.item和fromcantidad_recibidaepcOEntradaDetalle

我试过注释:

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__item: u'ITEM1',cantidad_recibida:Decimal('100')}:)

pendientes = OEntradaDetalle.objects.filter(
oentrada__folio=folio_orden_entrada,cantidad_recibida__gt=0
).exclude(epc__in=inventario).values(
'epc','producto__item','cantidad_recibida')

实际上这就是我现在所拥有的:(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(不包括fields, model and pk信息),那就太棒了。

谢谢!!

4

1 回答 1

0

You can do this in your 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
于 2014-11-06T03:15:17.560 回答