Skip to content

Commit

Permalink
[3.2.x] Used backend vendors in custom model fields docs.
Browse files Browse the repository at this point in the history
Backport of d7394cf from main
  • Loading branch information
felixxm committed Aug 24, 2021
1 parent 358e65a commit d95a014
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/howto/custom-model-fields.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,14 @@ If you aim to build a database-agnostic application, you should account for
differences in database column types. For example, the date/time column type
in PostgreSQL is called ``timestamp``, while the same column in MySQL is called
``datetime``. You can handle this in a :meth:`~Field.db_type` method by
checking the ``connection.settings_dict['ENGINE']`` attribute.
checking the ``connection.vendor`` attribute. Current built-in vendor names
are: ``sqlite``, ``postgresql``, ``mysql``, and ``oracle``.

For example::

class MyDateField(models.Field):
def db_type(self, connection):
if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql':
if connection.vendor == 'mysql':
return 'datetime'
else:
return 'timestamp'
Expand Down

0 comments on commit d95a014

Please sign in to comment.