0

I have a postgres database storing all versions of different pip packages.

The format looks something like X.X.X (e.g. 01.9.1 or 14.12.03) and is a string. The number X can be something between 1 and a finite number n.

I would now like to read them in my Django app and order the versions to get the newest version by package.

My first idea was something like: Version.objects.filter(package='example').order_by('version').last().

The problem is, this returns '0.9.1' instead of '0.21.1' if we have these two versions as an example.

Is there an easy way to get the ordering correct?

This would mean:

1.) Order by highest number before the first dot

2.) Order by highest number in the middle section

3.) Order by highest number after the second dot

  • 1
    possible duplication: https://stackoverflow.com/questions/11887762/how-do-i-compare-version-numbers-in-python/21065570 – cizario Oct 18 '21 at 19:44
  • Thanks for the link. Using packaging.version.parse should do the ordering properly. My problem is, that I would like to do this on database/queryset level if possible, without having to loop over each version in python. – ddjjaannggoo Oct 19 '21 at 07:10

0 Answers0