Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#2191 closed defect (wontfix)

Ordering of IpAddressField ist not natural

Reported by: django@… Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


The ordering of IpAddressField is not natural which doesn't look very well on large lists.

I think for platform and backwards compatibility a change to a interger field are out of scope.

I found which would allow at least with mysql a natural sorting of IpAddressFields. For Postgresql i haven't found a function yet, only for the inet/cidr row types.

Change History (3)

comment:1 Changed 12 years ago by Adrian Holovaty

Resolution: wontfix
Status: newclosed

I think this is one we're just going to have to ignore and live with. It'd be too much of a special case to get it fixed.

comment:2 Changed 12 years ago by django@…

Year :)
I just was wondering, are there any other types of fields that don't sort correctly, so it would be good to add some generic functionality that would allow fields to be sorted based on type and database ?

comment:3 Changed 12 years ago by anonymous

I was also fighting with this issue yesterday. I was able to get the sorting to work by using an integer field, which was generated on each save:

class SomeModel(models.Model):
  ip_address = models.CharField(maxlength=255)
  bin_ip = models.IntegerField()

  def save(self):
    sets = map(int, self.ip_address.split("."))
    self.bin_ip = sets[0]*256**3 + sets[1]*256**2 + sets[2]*256 + sets[3]
    print self.bin_ip
    super(IP, self).save()

Now, to order all SomeModels by IP, one must order by bin_ip, not by ip_address. This works and only downside I have found so far is that the bin_ip must be included in Admin list_display to allow sorting by IP manually. However, if the IP would be saved as an integer that could be converted back to IP address, I think that a custom field would make it.

Note: See TracTickets for help on using tickets.
Back to Top