Commits

Owen Nelson committed 833af8e

making query making more flexible for CIDR lookups by taking string args instead of IP instances

Comments (0)

Files changed (2)

ipyfield/models.py

             return None
         return value.int()
 
+    def get_prep_lookup(self, lookup_type, value):
+        value = self.to_python(value) # Ensure we have IP instance or None
+        if lookup_type == 'exact':
+            return self.get_prep_value(value)
+        elif lookup_type == 'in':
+            return [self.get_prep_value(v) for v in value]
+        else:
+            raise TypeError('Lookup type %r not supported.' % lookup_type)
+
     def get_internal_type(self):
         return 'BigIntegerField'
 

ipyfield/tests.py

 class DummyModel(Model):
     field = IPyField()
 
+    def __str__(self):
+        return '<DummyModel %s>' % self.pk
+
+    __repr__ = __str__
+
 class IPyFieldTests(TestCase):
 
     def setUp(self):
                .count())
         self.assertEqual(4, DummyModel.objects.filter(
                field__in=IP('127.0.0.0/30')).count())
+        # testing lookups work with str as __in param rather than 
+        # requiring an IP instance
+        self.assertQuerysetEqual(
+               DummyModel.objects.filter(field__in=IP('127.0.0.0/30')),
+               [repr(o) for o in DummyModel.objects.filter(
+                                            field__in='127.0.0.0/30')])
 
 
+