Commits

Michael Manfre committed ad8b419

Issue 63: Added field BigAutoField and changed BigIntegerField to work with python type long instead of int.

Comments (0)

Files changed (3)

source/sqlserver_ado/creation.py

 class DatabaseCreation(BaseDatabaseCreation):
     data_types = {
         'AutoField':            'int IDENTITY (1, 1)',
+        'BigAutoField':         'bigint IDENTITY (1, 1)',
         'BigIntegerField':      'bigint',
         'BooleanField':         'bit',
         'CharField':            'nvarchar(%(max_length)s)',

source/sqlserver_ado/fields.py

 """This module provides SQL Server specific fields for Django models."""
-from django.db.models import IntegerField
+from django.db.models import IntegerField, AutoField
 
 class BigIntegerField(IntegerField):
     """A BigInteger field, until Django ticket #399 lands (if ever.)"""
     def get_internal_type(self):
         return "BigIntegerField"
+
+    def to_python(self, value):
+        if value is None:
+            return value
+        try:
+            return long(value)
+        except (TypeError, ValueError):
+            raise exceptions.ValidationError(
+                _("This value must be an long."))
+
+    def get_db_prep_value(self, value):
+        if value is None:
+            return None
+        return long(value)
+
+class BigAutoField(AutoField):
+    """A bigint IDENTITY field"""
+    def get_internal_type(self):
+        return "BigAutoField"
+
+    def to_python(self, value):
+        if value is None:
+            return value
+        try:
+            return long(value)
+        except (TypeError, ValueError):
+            raise exceptions.ValidationError(
+                _("This value must be an long."))
+
+    def get_db_prep_value(self, value):
+        if value is None:
+            return None
+        return long(value)

tests/test_main/regressiontests/models.py

 from django.db import models, IntegrityError
 from django.test import TestCase
 from django.core.paginator import Paginator
+from sqlserver_ado.fields import BigAutoField, BigIntegerField
 
 class Bug19Table(models.Model):
     """ A simple model for testing string comparisons.
         
         q = list(Bug62Table.objects.exclude(email=u''))
         self.assertEqual(len(q), 2)
+
+class Bug63Table(models.Model):
+    """
+    Test that the BigAutoField and BigIntegerField fields work.
+
+    >>> Bug63Table(number=2147483648L).save()
+    >>> len(list(Bug63Table.objects.all()))
+    1
+    >>> big = Bug63Table.objects.get(number=2147483648L)
+    >>> big.number
+    2147483648L
+    """
+    id = BigAutoField(primary_key=True)
+    number = BigIntegerField()