简体   繁体   English

Django:queryset“丢失”一个值

[英]Django: queryset “loosing” a value

I have 2 models, one with a list of clients and the other with a list of sales. 我有2种型号,一种带有客户列表,另一种带有销售列表。 My intention is to add sales rank value to the clients queryset. 我的意图是将销售排名值添加到客户的查询集中。

all_clients = Contactos.objects.values("id", "Vendedor", "codigo", 'Nombre', "NombrePcia", "Localidad", "FechaUltVenta")
sales = Ventas.objects.all()

Once loaded I aggregate all the sales per client summing the subtotal values of their sales and then order the result by their total sales. 加载后,我将汇总每个客户的所有销售额,将其销售额的小计值相加,然后按其总销售额对结果进行排序。

sales_client = sales.values('cliente').annotate(
    fact_total=Sum('subtotal'))

client_rank = sales_client .order_by('-fact_total')

Then I set the rank of those clients and store the value in a the "Rank" values in the same client_rank queryset. 然后,我设置那些客户端的等级,并将值存储在同一client_rank查询集中的“等级”值中。

a = 0
for rank in client_rank:
    a = a + 1
    rank['Rank'] = a

Everything fine up to now. 到目前为止一切都很好。 When I print the results in the template I get the expected values in the "client_rank" queryset: "client name" + "total sales per client" + "Rank". 当我在模板中打印结果时,我在“ client_rank”查询集中获得了预期的值:“客户名称” +“每位客户的总销售额” +“排名”。

{'cliente': '684 DROGUERIA SUR', 'fact_total': Decimal('846470'), 'Rank': 1}
{'cliente': '699 KINE ESTETIC', 'fact_total': Decimal('418160'), 'Rank': 2}
etc....

The problem starts here 问题从这里开始

First we should take into account that not all the clients in the "all_clients" queryset have actual sales in the "sales" queryset. 首先,我们应该考虑到并非“ all_clients”查询集中的所有客户在“ sales”查询集中都具有实际销售额。 So I must find which ones do have sales, assign them the "Rank" value and a assign a standard value for the ones who don´t. 因此,我必须找到哪些有销售量,为他们分配“等级”值,为没有销售量的人分配标准值。

    for subject in all_clients:
    subject_code = str(client["codigo"])
    try:
        selected_subject = ranking_clientes.get(cliente__icontains=subject_code)
        subject ['rank'] = selected_subject['Rank']
    except:
        subject ['rank'] = "Some value"

The Try always fails because "selected_subject" doesn´t seems to hace the "Rank" value. 尝试始终失败,因为“ selected_subject”似乎没有“ Rank”值。 If I print the "selected_subject" I get the following: 如果打印“ selected_subject”,则会得到以下信息:

{'cliente': '904 BAHIA BLANCA BASKET', 'fact_total': Decimal('33890')}

Any clues on why I´, lossing the "Rank" value? 关于我为什么失去“等级”价值的任何线索? The original "client_rank" queryset still has that value included. 原始的“ client_rank”查询集仍包含该值。

Thanks! 谢谢!

I presume that ranking_clientes is the same as client_rank . 我认为ranking_clientesclient_rank相同。

The problem is that .get will always do a new query against the database. 问题是.get将始终对数据库执行新查询。 This means that any modifications you made to the dictionaries returned in the original query will not have been applied to the result of the get call. 这意味着您对原始查询中返回的字典所做的任何修改都不会应用于get调用的结果。

You would need to iterate through your query to find the one you need: 您将需要遍历查询以找到所需的查询:

selected_subject = next(client for client in ranking_clientes if subject_code in client.cliente)

Note, this is pretty inefficient if you have a lot of clients. 注意,如果您有很多客户,这效率很低。 I would rethink your model structure. 我会重新考虑您的模型结构。 Alternatively, you could look into using a database function to return the rank directly as part of the original query. 或者,您可以考虑使用数据库函数直接将排名作为原始查询的一部分返回。

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

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