Add a `default` accessor to connection handlers for db, cache, and tasks
- Dominant language
- No language data
- Stars
- 188
- Forks
- 7
- PR merge metrics
- No merged PRs in 30d
Description
### Code of Conduct
- [x] I agree to follow Django's Code of Conduct
### Feature Description
Add a `default` property to the connection handlers in `django.db`, `django.core.cache`, and `django.tasks`, so the default alias is reachable as `connections.default`, `caches.default`, and `task_backends.default`. `mailers.default` already works this way.
### Problem
I opened ticket-37345 after reaching for `connections.default` while migrating a project, having just learned about `mailers.default`. It raises `AttributeError: 'ConnectionHandler' object has no attribute 'default'`, with no hint about what to use instead.
DEP 0018 describes `mailers.default` as the parallel of `django.db.connection` and `django.core.cache.cache`. The other handlers never got the matching accessor, so the same operation is spelled four different ways:
| Subsystem | Default accessor |
|---|---|
| mail | `mailers.default` |
| db | `connection` or `connections["default"]` |
| cache | `cache` or `caches["default"]` |
| tasks | `default_task_backend` or `task_backends["default"]` |
Mike Edmunds agreed in the ticket that this gap is worth closing and asked for a separate issue. Since DEP 0018 was about consistency, a missing `.default` looks more like unfinished work than a deliberate choice. I know Django reasonably well and still got it wrong, so newcomers will have a harder time.
### Request or proposal
proposal
### Additional Details
- Original report: ticket-37345
- [DEP 0018: Mailers](https://github.com/django/deps/blob/main/accepted/0018-mailers.md)
### Implementation Suggestions
`MailersHandler.default` is a plain property (`return self[DEFAULT_MAILER_ALIAS]`) rather than a `LazyObject` or `ConnectionProxy`, because backend instances are not cacheable. The other handlers can use the same property, and they share a base class, so one implementation covers all three:
```
python
# django/utils/connection.py
class BaseConnectionHandler:
default_alias = None
...
@property
def default(self):
return self[self.default_alias]
```
Each subclass then sets its own constant, so the alias stays out of the base class and each subsystem can change it independently:
- `ConnectionHandler` (`django/db/utils.py`): `default_alias = DEFAULT_DB_ALIAS`
- `CacheHandler` (`django/core/cache/__init__.py`): `default_alias = DEFAULT_CACHE_ALIAS`
- `TaskBackendHandler` (`django/tasks/__init__.py`): `default_alias = DEFAULT_TASK_BACKEND_ALIAS`
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with BaseConnectionHandler in django/utils/connection.py and compare it with MailersHandler.default. Inspect ConnectionHandler in django/db/utils.py, CacheHandler in django/core/cache/__init__.py, and TaskBackendHandler in django/tasks/__init__.py, including their default-alias constants. Done means the three handlers expose .default for their default alias while existing access patterns continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100