Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#553 closed defect (fixed)

Adding fields is non-trivial due to lack of database data type generic mapping

Reported by: wojtek3@… Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: wojtek3@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I added a field named GenericImageField inheriting from field.ImageField and here is the error I got when trying to install the app which has a model using that field:

  File "d:\django\django\core\", line 209, in get_sql_all
    return get_sql_create(mod) + get_sql_initial_data(mod)
  File "d:\django\django\core\", line 68, in get_sql_create
    col_type = db.DATA_TYPES[data_type]
KeyError: 'GenericImageField'

So in order for this to work I would have to modify db.DATA_TYPES for the chosen DB. This is very non-nice. A suggested and simple solution is adding a function to the Field class which would return the 'internal data type':

class Field:
   def internal_data_type(self):
      return self.__class__.__name__

so that one could override it in the new field and set it to return the superclasses name as the internal data type. This would also let you narrow the amount of data types. Perhaps using to find out the data type should be skipped alltogether and an array of standard database-independent data types should be introduced. Those datatypes could be then associated to database-dependent data types in the database driver. java.sql is a good example here, and using class names as names for those 'generic data types' as it is done now is not very nice.

Change History (3)

comment:1 Changed 13 years ago by Adrian Holovaty

Status: newassigned

I agree that using __class__.__name__ can be improved on. Your internal_data_type patch is a good one. Anybody see any problems with it?

comment:2 Changed 13 years ago by Jacob

Sounds good to me.

comment:3 Changed 13 years ago by Adrian Holovaty

Resolution: fixed
Status: assignedclosed

(In [713]) Fixed #553 -- Added django.core.meta.fields.Field.get_internal_type() hook, for creating custom meta.Field subclasses. Thanks, wojtek3

Note: See TracTickets for help on using tickets.
Back to Top