Opened 9 years ago

Closed 9 years ago

Last modified 8 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
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:

Description

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\management.py", line 209, in get_sql_all
    return get_sql_create(mod) + get_sql_initial_data(mod)
  File "d:\django\django\core\management.py", 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 .class.name 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 9 years ago by adrian

  • Status changed from new to assigned

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 9 years ago by jacob

Sounds good to me.

comment:3 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from assigned to closed

(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