From 85ef98dc6ec565b1add417bd76808664e7318026 Mon Sep 17 00:00:00 2001 From: Aron Podrigal Date: Mon, 3 Aug 2015 08:03:41 -0400 Subject: Fixed #24305 -- Allowed overriding fields on abstract models. Fields inherited from abstract base classes may be overridden like any other Python attribute. Inheriting from multiple models/classes with the same attribute name will follow the MRO. --- docs/releases/1.10.txt | 2 ++ docs/topics/db/models.txt | 34 +++++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'docs') diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 9e24225173..35cf1d42c3 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -400,6 +400,8 @@ Models app label and class interpolation using the ``'%(app_label)s'`` and ``'%(class)s'`` strings. +* Allowed overriding model fields inherited from abstract base classes. + * The :func:`~django.db.models.prefetch_related_objects` function is now a public API. diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt index 2850294581..1dc1b17202 100644 --- a/docs/topics/db/models.txt +++ b/docs/topics/db/models.txt @@ -1376,11 +1376,35 @@ Field name "hiding" is not permitted ------------------------------------- In normal Python class inheritance, it is permissible for a child class to -override any attribute from the parent class. In Django, this is not permitted -for attributes that are :class:`~django.db.models.Field` instances (at -least, not at the moment). If a base class has a field called ``author``, you -cannot create another model field called ``author`` in any class that inherits -from that base class. +override any attribute from the parent class. In Django, this isn't usually +permitted for model fields. If a non-abstract model base class has a field +called ``author``, you can't create another model field or define +an attribute called ``author`` in any class that inherits from that base class. + +This restriction doesn't apply to model fields inherited from an abstract +model. Such fields may be overridden with another field or value, or be removed +by setting ``field_name = None``. + +.. versionchanged:: 1.10 + + The ability to override abstract fields was added. + +.. warning:: + + Model managers are inherited from abstract base classes. Overriding an + inherited field which is referenced by an inherited + :class:`~django.db.models.Manager` may cause subtle bugs. See :ref:`custom + managers and model inheritance `. + +.. note:: + + Some fields define extra attributes on the model, e.g. a + :class:`~django.db.models.ForeignKey` defines an extra attribute with + ``_id`` appended to the field name, as well as ``related_name`` and + ``related_query_name`` on the foreign model. + + These extra attributes cannot be overridden unless the field that defines + it is changed or removed so that it no longer defines the extra attribute. Overriding fields in a parent model leads to difficulties in areas such as initializing new instances (specifying which field is being initialized in -- cgit v1.3