简体   繁体   English

Django在查询集返回的字典列表中搜索字符串匹配项。

[英]Django searching for a string match in a list of dictionary which is returned from queryset.

I have something like this in my view.py 我的view.py中有类似的内容

def calculateMark(mobile_a, mobile_b):
    #variables
    mobile_a = mobile_a
    mobile_b = mobile_b

    results_a = []
    results_b = []
    record_a = TechSpecificationAdd.objects.filter(mobile_name=mobile_a).values()
    record_b = TechSpecificationAdd.objects.filter(mobile_name=mobile_b).values()
    results_a += record_a
    results_b += record_b
    record_a = record_a[0]
    record_b = record_b[0]

    if 'Android' in record_a['os']:
        os_mark_a = 8.9
    elif 'Android' in record_b['os']:
        os_mark_b = 8.9
    elif 'iOS' in record_a['os']:
        os_mark_a = 14
    if 'iOS' in record_b['os']:
        os_mark_b = 14
    else:
        os_mark_a = 1
        os_mark_b = 1

calculateMark Function will calculate a mark for mobiles. computeMark函数将为手机计算一个标记。 Everything is ok but in case of Operating System (os) testing. 一切正常,但要进行操作系统(os)测试。 Its returning always the else block. 它总是返回else块。

The question is how can I search for a string like "ios" in my record['os'] field? 问题是如何在我的record ['os']字段中搜索类似“ ios”的字符串?

Correct me if I'm wrong, but I believe your problem is the if in the following line: 如果我错了,请纠正我,但我认为您的问题是以下行中的if

if 'iOS' in record_b['os']:

As a result of this code in every case other than iOS in record_b , both os_mark_a and os_mark_b will be set to 1 . 作为该代码在比其他的iOS每一种情况下的结果record_b ,既os_mark_aos_mark_b将被设置为1 Even if they were set to another value before, they will be overwritten by 1 . 即使之前将它们设置为另一个值,它们也将被1覆盖。 To correct this particular problem change the line to: 要更正此特定问题,请将行更改为:

elif 'iOS' in record_b['os']:

There are other things I find weird: 我还有其他奇怪的事情:

  • In most cases you set just one of os_mark_b or os_mark_a , leaving the other one unset. 在大多数情况下,您只设置了os_mark_bos_mark_a的一个,而另一个未设置。 You could correct this for example by settings both to a default value first. 例如,您可以通过将两者都设置为默认值来更正此问题。

  • You don't need to load the values to a dictionary using values() - just use .get() and use the resulting object directly. 您不需要使用values()将值加载到字典中-只需使用.get()即可直接使用结果对象。 Among other benefits this would eliminate lines 6, 7, 10, 11, 12, 13 completely. 除其他好处外,这将完全消除第6、7、10、11、12、13行。

  • Calculations for A and B seem completely separate. A和B的计算似乎完全分开。 It seems it would be wiser to create a smaller method that calucaltes mark for a single object. 似乎更明智的做法是创建一个较小的方法,用calucaltes标记单个对象。

  • Your method doesn't return anything. 您的方法不返回任何内容。 I presume that's only because it's a simplified example. 我认为这仅仅是因为这是一个简化的示例。

  • What's the following code is about? 以下代码是关于什么的? That doesn't do anything... 那什么也没做...

     mobile_a = mobile_a mobile_b = mobile_b 

Update: Example of code without those issues: 更新:没有这些问题的代码示例:

def calculateMark(mobile):
    record = TechSpecificationAdd.objects.get(mobile_name=mobile)

    if 'Android' in record.os:
        return 8.9
    if 'iOS' in record.os:
        return 14
    return 1

Update 2: Also, since we already got so far in making this code better - this shouldn't be a separate function, but a method on a TechSpecificationAdd object. 更新2:另外,由于到目前为止我们已经在改进此代码方面做过很多工作-这不应该是一个单独的函数,而应该是TechSpecificationAdd对象上的方法。

class TechSpecificationAdd(models.Model):
    # some fields

    def calculateMark(self):
        if 'Android' in self.os:
            return 8.9
        if 'iOS' in self.os:
            return 14
        return 1

You would then use this in your code like this: 然后,您将在代码中使用以下代码:

record = TechSpecificationAdd.objects.get(mobile_name=mobile)
mark = record.calculateMark()

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

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