#35976: Document Options.original_attrs
-------------------------------+--------------------------------------
     Reporter:  Tim McCurrach  |                    Owner:  (none)
         Type:  Uncategorized  |                   Status:  new
    Component:  Documentation  |                  Version:  5.1
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  1              |                    UI/UX:  0
-------------------------------+--------------------------------------
Changes (by Tim McCurrach):

 * summary:  Persist original Meta class on Options instances => Document
     Options.original_attrs


Old description:

> When a `model.Model` class is created
> [https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/base.py#L112
> the metaclass `pop`s off] any `Meta` attribue defined on the model
> [https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/base.py#L122
> before calling] `super.__new__()`. We do
> [https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/base.py#L145
> pass it as an argument] to the `Options` class, and
> [https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/options.py#L136
> it's stored] on the `Options` instance, however
> [https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/options.py#L232
> this is then deleted] whilst the `Options.contribute_to_class` class is
> called. The attributes from the meta value are stored on a
> `original_attrs` attribute however. It might be a good idea to document
> this!!
>
> === Motivation for the documentation ===
>
> The motivation for this came change is born out of a desire to write a
> check that `db_table` is explicitly defined on all my models (even if it
> matches the default value that django would apply). I can write checks
> that require things like `ordering` is defined (or not defined) on a
> model. The problem with `db_table` is that defaults are applied when
> `contribute_to_class` is called. This means we need a reference to the
> underlying original Meta data. The `original_attrs` attribute is perfect
> for this, but as far as I can tell it isn't documented anywhere.

New description:

 When a `model.Model` class is created
 
[https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/base.py#L112
 the metaclass `pop`s off] any `Meta` attribue defined on the model
 
[https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/base.py#L122
 before calling] `super.__new__()`. We do
 
[https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/base.py#L145
 pass it as an argument] to the `Options` class, and
 
[https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/options.py#L136
 it's stored] on the `Options` instance, however
 
[https://github.com/django/django/blob/ded485464214a3f69b64402b7d82221279f80008/django/db/models/options.py#L232
 this is then deleted] whilst the `Options.contribute_to_class` class is
 called. The attributes from the meta value are stored on a
 `original_attrs` attribute however. It might be a good idea to document
 that this attribute exists.

 === Motivation for the documentation ===

 The motivation for this came change is born out of a desire to write a
 check that `db_table` is explicitly defined on all my models (even if it
 matches the default value that django would apply). I can write checks
 that require things like `ordering` is defined (or not defined) on a
 model. The problem with `db_table` is that defaults are applied when
 `contribute_to_class` is called. This means we need a reference to the
 underlying original Meta data. The `original_attrs` attribute is perfect
 for this, but as far as I can tell it isn't documented anywhere.

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35976#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070193a0a910a5-dcad773c-4eaa-4157-b226-0067ad4ec89d-000000%40eu-central-1.amazonses.com.

Reply via email to