Changes between Initial Version and Version 3 of Ticket #35976
- Timestamp:
- Dec 7, 2024, 4:20:51 AM (11 days ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #35976
- Property Cc added
- Property Component Database layer (models, ORM) → Documentation
- Property Type New feature → Uncategorized
-
Ticket #35976 – Description
initial v3 1 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. 1 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!! 2 2 3 As far as I can tell, there is no reason why it is important to delete the original data stored in the class defined on the original model. Admitedly, perhaps it could be stored with a better name to indicate it is a non-functional reference to the original `Meta` class. `instance._meta.meta` probably doesn't make much sense, but `instance._meta.original_meta` might be more appropriate. 3 === Motivation for the documentation === 4 4 5 === Motivation for feature request === 6 7 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. This is not currently possible without subclassing the metaclass (`ModelBase`) which is a step further than would be ideal imo. 8 9 === The fix === 10 11 The fix should be very simple. We would just need to remove (or adapt) the line of code linked above which deletes the meta-class. (We could maybe use a better name than `original_meta`!!) 5 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.