Opened 13 days ago
Last modified 9 days ago
#35976 closed New feature
Persist original Meta class on Options instances — at Initial Version
Reported by: | Tim McCurrach | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.1 |
Severity: | Normal | Keywords: | |
Cc: | Tim McCurrach, Simon Charette | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
When a model.Model
class is created the metaclass `pop`s off any Meta
attribue defined on the model before calling super.__new__()
. We do pass it as an argument to the Options
class, and it's stored on the Options
instance, however this is then deleted whilst the Options.contribute_to_class
class is called.
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.
Motivation for feature request
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.
The fix
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
!!)