2014-11-05 41 views
0

我使用Django 1.7工作,我有這個型號序列化Django的查詢集與外國值

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.folioproducto.itemcantidad_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信息。

謝謝!

+0

我認爲[這](http://stackoverflow.com/questions/3753359/serializing-foreign-key-objects-in-django)相似對你的要求,如果你是爲了寧靜的目的,我建議使用Django Rest Framework。 – 2014-11-05 17:51:48

+0

我看到了,但我使用的Django 1.7和wadofstuff.django.serializers.json不工作了,我也試過natural_key,但沒有運氣 – 2014-11-05 18:02:58

+0

我也在使用Django Rest Framework,但是,我該如何做到這一點與REST? – 2014-11-05 18:05:42

回答

0

您可以在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 
+0

我試過這樣的東西,但這裏有另一個細節...我可以放置3個過濾條件?我需要一個'oentrada__folio'的產品,它的'cantidad_recibida'字段中的值大於0,並且'epc'沒有在庫存中註冊('filter(oentrada__folio = folio_orden_entrada, cantidad_recibida__gt = 0).exclude(epc__in = inventario )')。這個原因使我得到比API更「手動」的選擇 – 2014-11-06 15:11:01