Opened 8 years ago
Last modified 4 weeks ago
#27880 assigned Cleanup/optimization
Use __set_name__ to replace some usages of contribute_to_class.
Reported by: | Simon Charette | Owned by: | Clifford Gama |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | python3.6 |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If we were to define __set__name__ calling contribute_to_class()
on models.Field
, Manager
and Options
the following things could be done:
ModelBase.add_to_class
calls could be replaced by simplesetattr(cls, name, value)
.Options.contribute_to_class
could be inlined inOptions.__set_name__
.Manager.contribute_to_class
could be inlined inManager.__set_name__
.- There might be a way to inline
Field.contribute_to_class
by dealing with itsprivate_only
flag somehow.
Change History (7)
comment:1 by , 4 years ago
Triage Stage: | Someday/Maybe → Accepted |
---|
comment:2 by , 2 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 7 weeks ago
Has patch: | set |
---|---|
Needs documentation: | set |
Needs tests: | set |
comment:4 by , 7 weeks ago
Forum discussion https://forum.djangoproject.com/t/set-name-and-27880/35215
comment:5 by , 4 weeks ago
Closed https://github.com/django/django/pull/18621 as the work on Field.contribute_to_class()
was not production-ready, and as there was no feedback on the implementation in forum discussion.
comment:7 by , 4 weeks ago
Note that while Options.contribute_to_class()
is private API, searching /(?-i)Options/ django.db "def contribute_to_class" -path:django/db path:*py
in GitHub gives about 240 results, some of which implement their own Options
. These would need to rename contribute_to_class()
with __set_name__()
as this change is backward incompatible.
PR https://github.com/django/django/pull/18621