diff options
| author | Alexander Sosnovskiy <alecs.box@gmail.com> | 2015-11-13 10:56:10 +0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-12-04 07:55:41 -0500 |
| commit | b61eab18f71f3afb36b3e8cb208a145046b3d8f6 (patch) | |
| tree | 692a8dc30a5cad2ba66cb2d33b754390a136b210 /docs | |
| parent | 0e7d59df3ed6988cd5a45e26414c013ad40c7402 (diff) | |
Fixed #13774 -- Added models.Field.rel_db_type().
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/howto/custom-model-fields.txt | 33 | ||||
| -rw-r--r-- | docs/ref/models/fields.txt | 13 | ||||
| -rw-r--r-- | docs/releases/1.10.txt | 4 |
3 files changed, 41 insertions, 9 deletions
diff --git a/docs/howto/custom-model-fields.txt b/docs/howto/custom-model-fields.txt index ab36031fc0..2d370ef03a 100644 --- a/docs/howto/custom-model-fields.txt +++ b/docs/howto/custom-model-fields.txt @@ -374,14 +374,14 @@ For example:: else: return 'timestamp' -The :meth:`~Field.db_type` method is called by Django when the framework -constructs the ``CREATE TABLE`` statements for your application -- that is, -when you first create your tables. It is also called when constructing a -``WHERE`` clause that includes the model field -- that is, when you retrieve data -using QuerySet methods like ``get()``, ``filter()``, and ``exclude()`` and have -the model field as an argument. It's not called at any other time, so it can afford to -execute slightly complex code, such as the ``connection.settings_dict`` check in -the above example. +The :meth:`~Field.db_type` and :meth:`~Field.rel_db_type` methods are called by +Django when the framework constructs the ``CREATE TABLE`` statements for your +application -- that is, when you first create your tables. The methods are also +called when constructing a ``WHERE`` clause that includes the model field -- +that is, when you retrieve data using QuerySet methods like ``get()``, +``filter()``, and ``exclude()`` and have the model field as an argument. They +are not called at any other time, so it can afford to execute slightly complex +code, such as the ``connection.settings_dict`` check in the above example. Some database column types accept parameters, such as ``CHAR(25)``, where the parameter ``25`` represents the maximum column length. In cases like these, @@ -423,6 +423,23 @@ over this field. You are then responsible for creating the column in the right table in some other way, of course, but this gives you a way to tell Django to get out of the way. +The :meth:`~Field.rel_db_type` method is called by fields such as ``ForeignKey`` +and ``OneToOneField`` that point to another field to determine their database +column data types. For example, if you have an ``UnsignedAutoField``, you also +need the foreign keys that point to that field to use the same data type:: + + # MySQL unsigned integer (range 0 to 4294967295). + class UnsignedAutoField(models.AutoField): + def db_type(self, connection): + return 'integer UNSIGNED AUTO_INCREMENT' + + def rel_db_type(self, connection): + return 'integer UNSIGNED' + +.. versionadded:: 1.10 + + The :meth:`~Field.rel_db_type` method was added. + .. _converting-values-to-python-objects: Converting values to Python objects diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index e1b80d5fea..033ae14778 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -1701,7 +1701,8 @@ Field API reference where the arguments are interpolated from the field's ``__dict__``. - To map a ``Field`` to a database-specific type, Django exposes two methods: + To map a ``Field`` to a database-specific type, Django exposes several + methods: .. method:: get_internal_type() @@ -1717,6 +1718,16 @@ Field API reference See :ref:`custom-database-types` for usage in custom fields. + .. method:: rel_db_type(connection) + + .. versionadded:: 1.10 + + Returns the database column data type for fields such as ``ForeignKey`` + and ``OneToOneField`` that point to the :class:`Field`, taking + into account the ``connection``. + + See :ref:`custom-database-types` for usage in custom fields. + There are three main situations where Django needs to interact with the database backend and fields: diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 8b64eadde3..37bdf61d13 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -202,6 +202,10 @@ Models accessible as a descriptor on the proxied model class and may be referenced in queryset filtering. +* The new :meth:`Field.rel_db_type() <django.db.models.Field.rel_db_type>` + method returns the database column data type for fields such as ``ForeignKey`` + and ``OneToOneField`` that point to another field. + * The :attr:`~django.db.models.Func.arity` class attribute is added to :class:`~django.db.models.Func`. This attribute can be used to set the number of arguments the function accepts. |
