Ticket #12030: positive_integer.diff
File positive_integer.diff, 3.7 KB (added by , 14 years ago) |
---|
-
django/db/models/fields/__init__.py
978 978 979 979 class PositiveIntegerField(IntegerField): 980 980 description = _("Integer") 981 MAX_POSINT = 4294967295 981 982 982 983 def get_internal_type(self): 983 984 return "PositiveIntegerField" 984 985 985 986 def formfield(self, **kwargs): 986 defaults = {'min_value': 0} 987 defaults = {'min_value': 0, 988 'max_value': PositiveIntegerField.MAX_POSINT} 987 989 defaults.update(kwargs) 988 990 return super(PositiveIntegerField, self).formfield(**defaults) 989 991 -
tests/regressiontests/model_fields/tests.py
8 8 from django.db.models.fields.files import FieldFile 9 9 from django.utils import unittest 10 10 11 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Pos t, NullBooleanModel, BooleanModel, Document11 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, PosInt, Post, NullBooleanModel, BooleanModel, Document 12 12 13 13 # If PIL available, do these tests. 14 14 if Image: … … 299 299 b = BigInt.objects.get(value = '10') 300 300 self.assertEqual(b.value, 10) 301 301 302 303 class PositiveIntegerFieldTests(test.TestCase): 304 def test_limits(self): 305 # Ensure that values that are right at the limits can be saved 306 # and then retrieved whitout corruption. 307 maxval = 4294967295 308 minval = 0 309 PosInt.objects.create(value=maxval) 310 qs = PosInt.objects.filter(value__gte=maxval) 311 self.assertEqual(qs.count(), 1) 312 self.assertEqual(qs[0].value, maxval) 313 PosInt.objects.create(value=minval) 314 qs = PosInt.objects.filter(value__lte=minval) 315 self.assertEqual(qs.count(), 1) 316 self.assertEqual(qs[0].value, minval) 317 def test_types(self): 318 p = PosInt(value=0) 319 self.assertTrue(isinstance(p.value, (int, long))) 320 p.save() 321 self.assertTrue(isinstance(p.value, (int, long))) 322 p = PosInt.objects.all()[0] 323 self.assertTrue(isinstance(p.value, (int, long))) 324 325 def test_coercing(self): 326 PosInt.objects.create(value='10') 327 p = PosInt.objects.get(value = '10') 328 self.assertEqual(p.value, 10) 329 330 302 331 class TypeCoercionTests(test.TestCase): 303 332 """ 304 333 Test that database lookups can accept the wrong types and convert -
tests/regressiontests/model_fields/models.py
55 55 value = models.BigIntegerField() 56 56 null_value = models.BigIntegerField(null = True, blank = True) 57 57 58 class PosInt(models.Model): 59 value = models.PositiveIntegerField() 60 null_value = models.PositiveIntegerField(null = True, blank = True) 61 58 62 class Post(models.Model): 59 63 title = models.CharField(max_length=100) 60 64 body = models.TextField() -
AUTHORS
421 421 Manuel Saelices <msaelices@yaco.es> 422 422 Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/> 423 423 Vinay Sajip <vinay_sajip@yahoo.co.uk> 424 Bartolomé Sánchez <aliaselbarto@gmail.com> 424 425 Kadesarin Sanjek 425 426 Massimo Scamarcia <massimo.scamarcia@gmail.com> 426 427 Paulo Scardine <paulo@scardine.com.br>