#21702 closed Cleanup/optimization (fixed)
Consider providing apps.get_model("app_label.ModelName")
| Reported by: | Aymeric Augustin | Owned by: | nobody |
|---|---|---|---|
| Component: | Core (Other) | Version: | dev |
| Severity: | Normal | Keywords: | app-loading |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Basically:
--- a/django/apps/registry.py
+++ b/django/apps/registry.py
@@ -224,16 +224,21 @@ class Apps(object):
)
return model_list
- def get_model(self, app_label, model_name):
+ def get_model(self, app_label, model_name=None):
"""
Returns the model matching the given app_label and model_name.
+ As a shortcut, this function also accepts a single argument
+ <app_label>.<model_name>.
+
model_name is case-insensitive.
Raises LookupError if no application exists with this label, or no
model exists with this name in the application.
"""
self.populate_models()
+ if model_name is None:
+ app_label, _, model_name = app_label.partition('.')
return self.get_app_config(app_label).get_model(model_name.lower())
def register_model(self, app_label, model):
This would avoid repeating the splitting logic in many call sites.
Same question for get_registered_model.
Change History (5)
comment:1 by , 12 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 12 years ago
| Has patch: | set |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
PR: https://github.com/django/django/pull/2215
Anyone interested in reviewing it?
comment:3 by , 12 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
comment:5 by , 12 years ago
get_registered_model doesn't need the same treatment because it's used in only two places, it's a private API and I'd like to remove it eventually.
Note:
See TracTickets
for help on using tickets.
And also avoids repeating the logic in multiple external applications.