Opened 9 years ago
Closed 9 years ago
#26086 closed Cleanup/optimization (wontfix)
Don't allow admin.site.register([]) or admin.site.unregister([])
Reported by: | anabelensc | Owned by: | anabelensc |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | anabelensc | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Hello,
I think if you do that :
admin.site.register([]) or admin.site.unregister([]) the function register and unregister should complain.
I attach a pacth to try to fix this.
Thanks.
Attachments (1)
Change History (8)
by , 9 years ago
Attachment: | model_empty_list_registration_unregistration.diff added |
---|
comment:1 by , 9 years ago
Version: | 1.9 → master |
---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
Component: | Uncategorized → contrib.admin |
---|---|
Has patch: | set |
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
Maybe we can remove the check added in #25688 too. Are you able to submit your patch as a pull request?
follow-up: 6 comment:5 by , 9 years ago
Aymeric, could you give an example of such a pattern? I can't come up with a situation where an empty list would be inadvertently passed either, so I don't care much either way.
comment:6 by , 9 years ago
Replying to timgraham:
Aymeric, could you give an example of such a pattern? I can't come up with a situation where an empty list would be inadvertently passed either, so I don't care much either way.
If the list consist of models that can be present or not in a third-party application setting with optional dependencies.
models = [] for app_label in ['optional1', 'optional2']: try: app = apps.get_app_config(app_label) except LookupError: continue models.extend(app.get_models())
I've not used apps
this way personally but I've seen a couple of third party applications in the wild that rely on ImportError
to register or not optional dependency models to the admin.
Even if this pattern is not common I agree with Aymeric that it's not a good idea. Plus this is backward incompatible and would require a deprecation which is a lot of trouble for a check that could raise false positives.
comment:7 by , 9 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I'm not convinced that a good idea.
Some users may be passing a possibly-empty-list to
admin.site.register
in order to avoid having to special-case the empty list.