Refs #12663 -- Removed Meta API upgrade guide.
This commit is contained in:
parent
f434f5b84f
commit
dd367e0dae
|
@ -120,145 +120,3 @@ Retrieving all field instances of a model
|
||||||
<django.db.models.fields.DateTimeField: date_joined>,
|
<django.db.models.fields.DateTimeField: date_joined>,
|
||||||
<django.db.models.fields.related.ManyToManyField: groups>,
|
<django.db.models.fields.related.ManyToManyField: groups>,
|
||||||
<django.db.models.fields.related.ManyToManyField: user_permissions>)
|
<django.db.models.fields.related.ManyToManyField: user_permissions>)
|
||||||
|
|
||||||
.. _migrating-old-meta-api:
|
|
||||||
|
|
||||||
Migrating from the old API
|
|
||||||
==========================
|
|
||||||
|
|
||||||
As part of the formalization of the ``Model._meta`` API (from the
|
|
||||||
:class:`django.db.models.options.Options` class), a number of methods and
|
|
||||||
properties have been deprecated and will be removed in Django 1.10.
|
|
||||||
|
|
||||||
These old APIs can be replicated by either:
|
|
||||||
|
|
||||||
* invoking :meth:`Options.get_field()
|
|
||||||
<django.db.models.options.Options.get_field()>`, or;
|
|
||||||
|
|
||||||
* invoking :meth:`Options.get_fields()
|
|
||||||
<django.db.models.options.Options.get_fields()>` to retrieve a list of all
|
|
||||||
fields, and then filtering this list using the :ref:`field attributes
|
|
||||||
<model-field-attributes>` that describe (or retrieve, in the case of
|
|
||||||
``_with_model`` variants) the properties of the desired fields.
|
|
||||||
|
|
||||||
Although it's possible to make strictly equivalent replacements of the old
|
|
||||||
methods, that might not be the best approach. Taking the time to refactor any
|
|
||||||
field loops to make better use of the new API - and possibly include fields
|
|
||||||
that were previously excluded - will almost certainly result in better code.
|
|
||||||
|
|
||||||
Assuming you have a model named ``MyModel``, the following substitutions
|
|
||||||
can be made to convert your code to the new API:
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_field(name)`` becomes::
|
|
||||||
|
|
||||||
f = MyModel._meta.get_field(name)
|
|
||||||
|
|
||||||
then check if:
|
|
||||||
|
|
||||||
- ``f.auto_created == False``, because the new ``get_field()``
|
|
||||||
API will find "reverse" relations, and:
|
|
||||||
|
|
||||||
- ``f.is_relation and f.related_model is None``, because the new
|
|
||||||
``get_field()`` API will find
|
|
||||||
:class:`~django.contrib.contenttypes.fields.GenericForeignKey` relations.
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_field_by_name(name)`` returns a tuple of these four
|
|
||||||
values with the following replacements:
|
|
||||||
|
|
||||||
- ``field`` can be found by ``MyModel._meta.get_field(name)``
|
|
||||||
|
|
||||||
- ``model`` can be found through the
|
|
||||||
:attr:`~django.db.models.Field.model` attribute on the field.
|
|
||||||
|
|
||||||
- ``direct`` can be found by: ``not field.auto_created or field.concrete``
|
|
||||||
|
|
||||||
The :attr:`~django.db.models.Field.auto_created` check excludes
|
|
||||||
all "forward" and "reverse" relations that are created by Django, but
|
|
||||||
this also includes ``AutoField`` and ``OneToOneField`` on proxy models.
|
|
||||||
We avoid filtering out these attributes using the
|
|
||||||
:attr:`concrete <django.db.models.Field.concrete>` attribute.
|
|
||||||
|
|
||||||
- ``m2m`` can be found through the
|
|
||||||
:attr:`~django.db.models.Field.many_to_many` attribute on the field.
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_fields_with_model()`` becomes::
|
|
||||||
|
|
||||||
[
|
|
||||||
(f, f.model if f.model != MyModel else None)
|
|
||||||
for f in MyModel._meta.get_fields()
|
|
||||||
if not f.is_relation
|
|
||||||
or f.one_to_one
|
|
||||||
or (f.many_to_one and f.related_model)
|
|
||||||
]
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_concrete_fields_with_model()`` becomes::
|
|
||||||
|
|
||||||
[
|
|
||||||
(f, f.model if f.model != MyModel else None)
|
|
||||||
for f in MyModel._meta.get_fields()
|
|
||||||
if f.concrete and (
|
|
||||||
not f.is_relation
|
|
||||||
or f.one_to_one
|
|
||||||
or (f.many_to_one and f.related_model)
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_m2m_with_model()`` becomes::
|
|
||||||
|
|
||||||
[
|
|
||||||
(f, f.model if f.model != MyModel else None)
|
|
||||||
for f in MyModel._meta.get_fields()
|
|
||||||
if f.many_to_many and not f.auto_created
|
|
||||||
]
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_all_related_objects()`` becomes::
|
|
||||||
|
|
||||||
[
|
|
||||||
f for f in MyModel._meta.get_fields()
|
|
||||||
if (f.one_to_many or f.one_to_one)
|
|
||||||
and f.auto_created and not f.concrete
|
|
||||||
]
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_all_related_objects_with_model()`` becomes::
|
|
||||||
|
|
||||||
[
|
|
||||||
(f, f.model if f.model != MyModel else None)
|
|
||||||
for f in MyModel._meta.get_fields()
|
|
||||||
if (f.one_to_many or f.one_to_one)
|
|
||||||
and f.auto_created and not f.concrete
|
|
||||||
]
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_all_related_many_to_many_objects()`` becomes::
|
|
||||||
|
|
||||||
[
|
|
||||||
f for f in MyModel._meta.get_fields(include_hidden=True)
|
|
||||||
if f.many_to_many and f.auto_created
|
|
||||||
]
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_all_related_m2m_objects_with_model()`` becomes::
|
|
||||||
|
|
||||||
[
|
|
||||||
(f, f.model if f.model != MyModel else None)
|
|
||||||
for f in MyModel._meta.get_fields(include_hidden=True)
|
|
||||||
if f.many_to_many and f.auto_created
|
|
||||||
]
|
|
||||||
|
|
||||||
* ``MyModel._meta.get_all_field_names()`` becomes::
|
|
||||||
|
|
||||||
from itertools import chain
|
|
||||||
list(set(chain.from_iterable(
|
|
||||||
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
|
|
||||||
for field in MyModel._meta.get_fields()
|
|
||||||
# For complete backwards compatibility, you may want to exclude
|
|
||||||
# GenericForeignKey from the results.
|
|
||||||
if not (field.many_to_one and field.related_model is None)
|
|
||||||
)))
|
|
||||||
|
|
||||||
This provides a 100% backwards compatible replacement, ensuring that both
|
|
||||||
field names and attribute names ``ForeignKey``\s are included, but fields
|
|
||||||
associated with ``GenericForeignKey``\s are not. A simpler version would be::
|
|
||||||
|
|
||||||
[f.name for f in MyModel._meta.get_fields()]
|
|
||||||
|
|
||||||
While this isn't 100% backwards compatible, it may be sufficient in many
|
|
||||||
situations.
|
|
||||||
|
|
|
@ -49,9 +49,7 @@ The ``Model._meta`` object has been part of Django since the days of pre-0.96
|
||||||
"Magic Removal" -- it just wasn't an official, stable API. In recognition of
|
"Magic Removal" -- it just wasn't an official, stable API. In recognition of
|
||||||
this, we've endeavored to maintain backwards-compatibility with the old
|
this, we've endeavored to maintain backwards-compatibility with the old
|
||||||
API endpoint where possible. However, API endpoints that aren't part of the
|
API endpoint where possible. However, API endpoints that aren't part of the
|
||||||
new official API have been deprecated and will eventually be removed. A
|
new official API have been deprecated and will eventually be removed.
|
||||||
:ref:`guide to migrating from the old API to the new API
|
|
||||||
<migrating-old-meta-api>` has been provided.
|
|
||||||
|
|
||||||
Multiple template engines
|
Multiple template engines
|
||||||
-------------------------
|
-------------------------
|
||||||
|
@ -991,8 +989,7 @@ will work with both Django 1.8 and older versions::
|
||||||
for relation in opts.get_all_related_objects():
|
for relation in opts.get_all_related_objects():
|
||||||
to_model = getattr(relation, 'related_model', relation.model)
|
to_model = getattr(relation, 'related_model', relation.model)
|
||||||
|
|
||||||
Also note that ``get_all_related_objects()`` is deprecated in 1.8. See the
|
Also note that ``get_all_related_objects()`` is deprecated in 1.8.
|
||||||
:ref:`upgrade guide <migrating-old-meta-api>` for the new API.
|
|
||||||
|
|
||||||
Database backend API
|
Database backend API
|
||||||
--------------------
|
--------------------
|
||||||
|
@ -1222,9 +1219,6 @@ deprecated and will be removed in Django 1.10:
|
||||||
* ``get_fields_with_model()``
|
* ``get_fields_with_model()``
|
||||||
* ``get_m2m_with_model()``
|
* ``get_m2m_with_model()``
|
||||||
|
|
||||||
A :ref:`migration guide <migrating-old-meta-api>` has been provided to assist
|
|
||||||
in converting your code from the old API to the new, official API.
|
|
||||||
|
|
||||||
Loading ``cycle`` and ``firstof`` template tags from ``future`` library
|
Loading ``cycle`` and ``firstof`` template tags from ``future`` library
|
||||||
-----------------------------------------------------------------------
|
-----------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue