简体   繁体   中英

Django querysets, less than or greater than versioning number

My project model Software has a value called version with inputs like "3.2.4.98"

Now in my webapp you select 2 numbers eg. (0 , 4.2) And i grab the versions that are greater than or equal to 0 and less than or equal to 4.2. with a queryset like this to then show only data within the two versions :

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

versionA = 0 and versionB = 4.2 in this scenario. And this obviously doesn't work because it doesnt make comparisons with strings and multiple decimal digits like "3.2.4.98".

I take in the value of version as a string and save it to the model like so:

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()

And my views.py will give me a query set and give back context that is in between the 2 versions Views.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

The Problem:

How can i set up the version value so i am able to create a queryset like i did above and have it return if that number follows the >= and <= condition?

In a previous question i was told this way would help

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))

In the above equation i save the value as["3", "2" , "4", "98"] and then interate over it by comparing 2 lists one number at a time. But this doesn't seem possible to implement when creating a queryset.

How should i approach this problem?

This is soo simple.

Normally, versioning always goes upwards like:

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

Now lets convert this to:

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

Now just remove the dots and:

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

Simple, each three digits from behind represents the parts of the version.

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

Now store this numeric_version #1002003 instead of version #1.2.3 , in your database.

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

And query against it.

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

Note: version is an IntegerField

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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