简体   繁体   English

仅根据第一行的queryset过滤器-Django

[英]queryset filter according to only first line - Django

Tying to filter data using queryset.filter() in Django. 旨在在Django中使用queryset.filter()过滤数据。 but it returns not what I expecting. 但它没有返回我的期望。 can someone correct me. 有人可以纠正我。
single data cell looks like below.(each line separated by \\n) 单个数据单元如下所示(每行用\\ n分隔)

  1. こちらは1行です。(0.57)\\n です1行です。(0.57)\\ n
  2. こちらは2行です。(0.67)\\n です2行です。(0.67)\\ n
  3. こちらは3行です。(0.77)\\n です3行です。(0.77)\\ n
  4. こちらは4行です。(0.87)\\n です4行です。(0.87)\\ n
  5. こちらは5行です。(0.697) です5行です。(0.697)

code like below 如下代码

queryset = queryset.filter(predicted_result__regex = r"\A.*", predicted_result__contains='(0.5') |\
                   queryset.filter(predicted_result__regex = r"\A.*", predicted_result__contains='(0.6') |\
                   queryset.filter(predicted_result__regex = r"\A.*", predicted_result__contains='(0.7')

output: 输出:
this will be considering all 5 lines not only the first line. 这将考虑所有5行,而不仅仅是第一行。

target: 目标:
only get values contains in first line between score (inside brackets) 0.5 to 0.8. 仅获得包含在分数 (括号内) 0.5至0.8 之间的第一行中的 all the other lines should omit. 其他所有行都应省略。

expected result: 预期结果:

  1. こちらは1行です。(0.57)\\n です1行です。(0.57)\\ n

The problem with your current query is that your 'single data cell' is currently a string. 当前查询的问题是您的“单个数据单元格”当前为字符串。 Using a regex to calculate what may or may not be expected will still return the entire string on 'True'. 使用正则表达式计算可能会或可能不会发生的情况,仍会在'True'上返回整个字符串。 You should separate the 'text' from the 'float' values in order to query more efficiently. 您应该将“文本”和“浮点数”值分开,以便更有效地查询。

class Model(model):
    descriptor = models.CharField()
    value = models.FloatField()

Now you can actually query the 'value' on it's own. 现在,您实际上可以自行查询“值”。

If you are unable to change the model for whatever reason, you need to do multiple queries and use the union/intersection/difference methods. 如果由于某种原因无法更改模型,则需要执行多个查询并使用并集/相交/差异方法。

fives_queryset = queryset.filter(predicted_result__contains'(0.5')
sixes_queryset = queryset.filter(predicted_result__contains'(0.6')
sevens_queryset = queryset.filter(predicted_result__contains'(0.7')

result_queryset = union(fives_queryset, sixes_queryset, sevens_queryset)

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

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