Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#2191 closed defect (wontfix)

Ordering of IpAddressField ist not natural

Reported by: django@… Owned by: adrian
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:

Description

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 http://www.jimgrill.com/pages/content/sortip 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.

Attachments (0)

Change History (3)

comment:1 Changed 8 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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 8 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 8 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.