Datatypes

There are known issues related to Python/DB data types.

Dates and Times

When using Django-mssql with SQL Server 2005, all of the date related fields only support the datetime data type. Support for these legacy data types can be enabled using the use_legacy_date_fields option, or using the fields LegacyDateField, LegacyDateTimeField, and LegacyTimeField in sqlserver_ado.fields.

Note

:use_legacy_date_fields option has been deprecated and will be removed. Anyone still needing to use the ‘datetime’ data type must update their models to use the appropriate legacy model field.

To allow migrating specific apps or only some of your models to the new date times, the model fields DateField, DateTimeField, and TimeField in sqlserver_ado.fields use the new data types regardless of the use_legacy_date_fields option.

from django.db import models
from sqlserver_ado.fields import DateField, DateTimeField, TimeField

class MyModel(models.Model):
    # when use_legecy_date_fields is False, models.*Field will behave like these
    a_real_date = DateField() # date data type
    a_datetime2 = DateTimeField() # datetime2 data type
    a_real_time = TimeField() # time data type

    # when use_legecy_date_fields is True, models.*Field will behave like these
    a_date = LegacyDateField() # datetime data type
    a_datetime = LegacyDateTime() # datetime data type
    a_time = LegacyTimeField() # datetime data type

datetime limitations

With SQL Server 2005, only the datetime data type is usable with Django. This data type does not store enough precision to provide the full range of Python datetime dates and will round to increments of .000, .003, or .007 seconds. The earliest supported datetime date value is January 1, 1753.

SQL Server 2008 introduces a datetime2 type, with support for fractional seconds and the full range of Python datetime dates. To use this time, either set the use_legacy_date_fields option to False or use the sqlserver_ado.fields.DateTimeField with your models.

bigint

Prior to Django 1.3, bigint was not provided. This backend provided model fields to allow using the bigint datatype.

class sqlserver_ado.fields.BigAutoField

This is a django.db.models.AutoField for the bigint datatype.

class sqlserver_ado.fields.BigIntegerField

This was previously an django.db.models.IntegerField that specified the bigint datatype. As of Django 1.3, django.db.models.BigIntegerField is provided and should be used instead.

class sqlserver_ado.fields.BigForeignKey

This is a django.db.models.ForeignKey that should be used to reference either a BigAutoField or a BigIntegerField.

Note

If your (legacy) database using bigints for primary keys, then you’ll need to replace any introspected ForeignKey fields with BigForeignKey for things to work as expected.

money

The money and smallmoney data types will be introspected as DecimalField with the appropriate values for max_digits and decimal_places. This does not mean that they are expected to work without issue.

Unsupported Types

These types may behave oddly in a Django application, as they have limits smaller than what Django expects from similar types:

  • smalldatetime
  • tinyint
  • real