【问题标题】:Django querysets, less than or greater than versioning numberDjango 查询集,小于或大于版本号
【发布时间】:2019-08-15 23:55:56
【问题描述】:

我的项目模型 Software 有一个名为 version 的值,输入如“3.2.4.98”

现在在我的 webapp 中选择 2 个数字,例如。 (0 , 4.2) 我抓取大于或等于 0 且小于或等于 4.2 的版本。使用这样的查询集,然后只显示两个版本中的数据:

 Software.objects.filter(version__gte= self.versionA, version__lte=self.versionB) 

在这种情况下,versionA = 0 和 versionB = 4.2。这显然不起作用,因为它不会与字符串和多个十进制数字(如“3.2.4.98”)进行比较。

我将版本的值作为一个字符串并将其保存到模型中,如下所示:

def insert_created_bug_data(self, data):

        values = list((item['version']) for item in data['issues'])

        for x in values:
            insert_model = Software(key=bug[0])
            insert_model.save()

我的 views.py 会给我一个查询集并返回两个版本之间的上下文 视图.py

class VersionView(LoginRequiredMixin, TemplateView):
    template_name= 'app/version_report.html'

    def get(self, request, *args, **kwargs):
        self.versionA = request.GET.get('versionA','')
        self.versionB = request.GET.get('versionB','')
        return super().get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(VersionView, self).get_context_data(**kwargs)
        context['version_total'] = Software.objects.filter(version__gte= self.versionA, version__lte=self.versionB)
        return context

问题:

如何设置版本值,以便能够像上面那样创建查询集,并在该数字符合 >= 和

在上一个问题中,有人告诉我这种方式会有所帮助

a = "4.1.2.79"
b = "4.1.3.64"
#split it
a_list = a.split('.')
b_list = b.split('.')
#to look if a is bigger or not
a_bigger = False
#compare it number by number in list
for i in range(0, len(a_list)):
    #make from both lists one integer
    a_num = int(a_list[i])
    b_num = int(b_list[i])
    #if one num in the a_list is bigger
    if a_num > b_num:
        #make this variable True
        a_bigger = True
        #break the loop so it will show the results
    #else it wil look to the other nums in the lists
#print the result
print('a > b: %s' % str(a_bigger))

在上面的等式中,我将值保存为 ["3", "2" , "4", "98"],然后通过一次比较 2 个列表中的一个数字对其进行交互。但这在创建查询集时似乎无法实现。

我应该如何解决这个问题?

【问题讨论】:

  • 如何将Software 中的version 拆分为适当数量的字段,例如major_versionminor_version_1minor_version_2 等?这将使比较变得轻而易举(尤其是您可以在数据库中轻松进行比较)。
  • 应该可以为此定义新的查找字段(而不是 __gt 等,例如 __versiongt 在数据库中执行适当的逻辑。但我还不需要这样做并且没有时间弄清楚如何做。

标签: python django django-models


【解决方案1】:

这太简单了。

通常,版本控制总是向上的,例如:

1.0.0
1.0.1
1.0.xxx
...
1.234.xxx
1.52x.xxx
...
2.0.xx
3.99.xxx
89.23.456

现在让我们将其转换为:

001.000.000
001.000.001
001.000.xxx
...
001.234.xxx
001.52x.0xx
...
002.000.0xx
003.099.0xx
089.023.455

现在只需删除点和:

001000000
001000001
001000xxx
...
001234xxx
00152xxxx
...
0020000xx
0030990xx
089023456

简单,后面每三位代表版本的部分。

MAX_VERSION_PART_DIGITS = 3
def get_numeric_version(version):
    version_split = version.split(".")
    version_split = [str(vpart).zfill(MAX_VERSION_PART_DIGITS) for vpart in version_split]
    numeric_version=""
    return int(numeric_version.join(version_split))

print(get_numeric_version("1.2.3"))
print(get_numeric_version("12.3.45"))
print(get_numeric_version("12.345.6"))
#1002003
#12003045
#12345006

现在将 numeric_version #1002003 而不是 version #1.2.3 存储在您的数据库中。

Software.objects.create(version=get_numeric_version("1.2.3"))

并对其进行查询。

Software.objects.filter(version__gte= self.versionA, version__lte=self.versionB) 

注意:版本是IntegerField

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2013-01-10
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多