简体   繁体   English

Django:从queryset获取特定值

[英]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. 如果打印media_venta_mes我得到正确的计算。 For example: <QuerySet [{'prod_codigo': 'TP3 SHOULDER', 'media': 24}]> 例如: <QuerySet [{'prod_codigo': 'TP3 SHOULDER', 'media': 24}]>

Then I want to insert that media value in the general inventario_diferencia dictionary. 然后我想在普通的inventario_diferencia字典中插入该media值。

If I do media_venta_mes = media_venta_mes.values("media") I get <QuerySet [{'media': 24}]> . 如果我执行media_venta_mes = media_venta_mes.values("media")我会得到<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) . 如果我尝试使用media_venta_mes = media_venta_mes.get("media") ,这对我来说在聚合中起作用,我得到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' . 如果我尝试media_venta_mes = media_venta_mes.media我得到'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' . 我尝试使用media_venta_mes[0].media并获取'dict' object has no attribute 'media'

I Try media_venta_mes['media'] I get TypeError . 我试试media_venta_mes['media']我得到TypeError

I try media_venta_mes.first().media I get 'dict' object has no attribute 'media' 我试试media_venta_mes.first().media我得'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' 我得'dict' object has no attribute 'media'

If I try media_venta_mes.first()['media'] I get 'NoneType' object is not subscriptable 如果我尝试media_venta_mes.first()['media']我得'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. 我想你正在寻找media_venta_mes['media']media_venta_mes[0].media但我不确定我是否完全理解这个问题。 You may also be looking for the 'first' convenience method: https://docs.djangoproject.com/en/dev/ref/models/querysets/#first 您也可能正在寻找“第一个”便捷方法: 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. 小心尝试将变量设置为另一个具有相同名称的变量,即: media_venta_mes = media_venta_mes.media因为您会产生一些不良后果。

[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. 根据您的错误,您可以尝试使用media_venta_mes[0]['media']media_venta_mes.first()['media']因为这是从字典中获取元素的典型方法。

[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. 如果print(media_venta_mes.first())产生{'prod_codigo': 'TP3 SHOULDER', 'media': 24}media_venta_mes.first()['media']产生'NoneType' object is not subscriptable我在失利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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