#26080 closed Cleanup/optimization (wontfix)
Add field.on_delete to all relational fields
| Reported by: | Sven R. Kunze | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.8 |
| Severity: | Normal | Keywords: | |
| Cc: | tzanke@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
We still upgrade from 1.7 to 1.8 and replace all legacy API with get_fields().
Please consider the following loop:
for field in User._meta.get_fields():
if not field.is_relation:
continue
if field.on_delete != models.CASCADE:
continue
# do what's necessary
We've noticed field has not on_delete in all cases.
Wouldn't it make sense to have an on_delete attribute always available all the time if the field is a relation (even if it's None)?
Right now, we need to check for hasattr(field, 'on_delete') .
Change History (5)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Thanks for the suggestion. We are actually interested in the back relations. So, I need to check for ForeignObjectRel, don't I?
Please note, when checking for ForeignObjectRel, attribute on_delete is always set (to None if missing). So, it's exactly what we need.
Btw. it's somewhat confusing that get_field returns both user-defined and auto-generated back relations.
comment:4 by , 10 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
| Summary: | Missing field.on_delete → Add field.on_delete to all relational fields |
I'd say it's quasi-pubic. It's not documented but if we are to remove it, it probably needs to go through some deprecation as it's widely used. See #24317 for a discussion about it.
comment:5 by , 10 years ago
| Cc: | added |
|---|
As far as I know
on_deleteis only applicable toForeignKeyandOneToOneField(which inheritsOneToOneFieldso why not checkisinstance(field, ForeignKey)instead offield.is_relation?For what it's worth, I found this pattern in Django:
getattr(rel, 'on_delete', None) is CASCADE.I'm not convinced that
on_deletemust be a standard attribute of all relational fields.