简体   繁体   中英

Django: get specific value from queryset

I´m trying to get the media of sales per month. I can do the calculation but then I can´t get just the value instead a the full queryset.

I found different options and some work in some context but not in other. I guess there is some missunderstanding of different querysets data and dictionaries too.

Here goes my example.

 for este in inventario_diferencia:
        este['stock_valor_venta'] = este['existencias'] * este['valor_venta']
        este['diferencia'] = inventario_original.filter(fecha=diferencia_fecha, codigo_kinemed=este['codigo_kinemed']).values()[0]['existencias'] - este['existencias']
        media_venta_mes = Ventas.objects.filter(prod_codigo=este['codigo_kinemed']).values("prod_codigo").annotate(media=Sum("uds")/Count(TruncMonth('fecha'), distinct=True))

If a print media_venta_mes I get the correct calculation. For example: <QuerySet [{'prod_codigo': 'TP3 SHOULDER', 'media': 24}]>

Then I want to insert that media value in the general inventario_diferencia dictionary.

If I do media_venta_mes = media_venta_mes.values("media") I get <QuerySet [{'media': 24}]> .

If I try media_venta_mes = media_venta_mes.get("media") , which works for me in aggregates, I get too many values to unpack (expected 2) .

If I try media_venta_mes = media_venta_mes.media I get 'QuerySet' object has no attribute 'media' .

I tryied something more I can´t document now. I know I´m beign a dumb newbie here, sorry and thanks for the help.

Tests update

I try media_venta_mes[0].media and get 'dict' object has no attribute 'media' .

I Try media_venta_mes['media'] I get TypeError .

I try media_venta_mes.first().media I get 'dict' object has no attribute 'media'

If I Try

media_venta_mes = Ventas.objects.filter(prod_codigo=este['codigo_kinemed']).values("prod_codigo").annotate(media=Sum("uds")/Count(TruncMonth('fecha'), distinct=True)).first()

este['diferencia_mes'] = media_venta_mes.media

I get 'dict' object has no attribute 'media'

If I try media_venta_mes.first()['media'] I get 'NoneType' object is not subscriptable

I think you are looking for media_venta_mes['media'] or media_venta_mes[0].media but I'm not sure I completely understand the question. You may also be looking for the 'first' convenience method: https://docs.djangoproject.com/en/dev/ref/models/querysets/#first

Essentially, you want to get the first record of the queryset, then get the attribute.

Be careful in trying to set a variable to another with the same name ie: media_venta_mes = media_venta_mes.media as you'll have some undesired consequences.

[EDIT]

based on your errors, can you try to use media_venta_mes[0]['media'] or media_venta_mes.first()['media'] as that is the typical way to get an element from a dictionary.

[Edit]

if print(media_venta_mes.first()) produces {'prod_codigo': 'TP3 SHOULDER', 'media': 24} but media_venta_mes.first()['media'] yields 'NoneType' object is not subscriptable I'm at a loss.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM