Add introspection support for BinaryField

This commit is contained in:
Claude Paroz 2012-12-17 22:35:35 +01:00
parent d680a3f447
commit c5a25c2771
5 changed files with 10 additions and 2 deletions

View File

@ -7,6 +7,7 @@ foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REF
class DatabaseIntrospection(BaseDatabaseIntrospection): class DatabaseIntrospection(BaseDatabaseIntrospection):
# Maps type objects to Django Field types. # Maps type objects to Django Field types.
data_types_reverse = { data_types_reverse = {
cx_Oracle.BLOB: 'BinaryField',
cx_Oracle.CLOB: 'TextField', cx_Oracle.CLOB: 'TextField',
cx_Oracle.DATETIME: 'DateField', cx_Oracle.DATETIME: 'DateField',
cx_Oracle.FIXED_CHAR: 'CharField', cx_Oracle.FIXED_CHAR: 'CharField',

View File

@ -7,6 +7,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
# Maps type codes to Django Field types. # Maps type codes to Django Field types.
data_types_reverse = { data_types_reverse = {
16: 'BooleanField', 16: 'BooleanField',
17: 'BinaryField',
20: 'BigIntegerField', 20: 'BigIntegerField',
21: 'SmallIntegerField', 21: 'SmallIntegerField',
23: 'IntegerField', 23: 'IntegerField',

View File

@ -30,6 +30,7 @@ class FlexibleFieldLookupDict(object):
'real': 'FloatField', 'real': 'FloatField',
'text': 'TextField', 'text': 'TextField',
'char': 'CharField', 'char': 'CharField',
'blob': 'BinaryField',
'date': 'DateField', 'date': 'DateField',
'datetime': 'DateTimeField', 'datetime': 'DateTimeField',
'time': 'TimeField', 'time': 'TimeField',

View File

@ -10,6 +10,7 @@ class Reporter(models.Model):
last_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
email = models.EmailField() email = models.EmailField()
facebook_user_id = models.BigIntegerField(null=True) facebook_user_id = models.BigIntegerField(null=True)
raw_data = models.BinaryField(null=True)
class Meta: class Meta:
unique_together = ('first_name', 'last_name') unique_together = ('first_name', 'last_name')

View File

@ -60,9 +60,13 @@ class IntrospectionTests(TestCase):
def test_get_table_description_types(self): def test_get_table_description_types(self):
cursor = connection.cursor() cursor = connection.cursor()
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table) desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
# The MySQL exception is due to the cursor.description returning the same constant for
# text and blob columns. TODO: use information_schema database to retrieve the proper
# field type on MySQL
self.assertEqual( self.assertEqual(
[datatype(r[1], r) for r in desc], [datatype(r[1], r) for r in desc],
['IntegerField', 'CharField', 'CharField', 'CharField', 'BigIntegerField'] ['IntegerField', 'CharField', 'CharField', 'CharField',
'BigIntegerField', 'BinaryField' if connection.vendor != 'mysql' else 'TextField']
) )
# The following test fails on Oracle due to #17202 (can't correctly # The following test fails on Oracle due to #17202 (can't correctly
@ -85,7 +89,7 @@ class IntrospectionTests(TestCase):
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table) desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
self.assertEqual( self.assertEqual(
[r[6] for r in desc], [r[6] for r in desc],
[False, False, False, False, True] [False, False, False, False, True, True]
) )
# Regression test for #9991 - 'real' types in postgres # Regression test for #9991 - 'real' types in postgres