Reputation: 5298
I have:
class Range(models.Model):
ip = models.IntegerField() # as produced by socket.inet_aton + struct.unpack
mask = models.IntegerField()
Given a certain IP, how can I get all the ranges that match this specific IP using the Django models?
If I were using raw SQL, I would use the database's bitwise operators, but the Django ORM doesn't support those.
Upvotes: 2
Views: 2712
Reputation: 3628
I would recommend using python's iptools :
http://code.google.com/p/python-iptools/
Upvotes: 0
Reputation: 33200
To do faster query fitting the range you'd better store lower and upper IP of range as integer. Then selection of needed objects should be as simple as Range.objects.filter(ip_low__le=ip, ip_up__ge=ip)
.
Upvotes: 2