Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#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 Tim Graham, 9 years ago

As far as I know on_delete is only applicable to ForeignKey and OneToOneField (which inherits OneToOneField so why not check isinstance(field, ForeignKey) instead of field.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_delete must be a standard attribute of all relational fields.

comment:2 by Sven R. Kunze, 9 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:3 by Sven R. Kunze, 9 years ago

Is ForeignObjectRel part of the official API?

comment:4 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed
Summary: Missing field.on_deleteAdd 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 TZanke, 9 years ago

Cc: tzanke@… added
Note: See TracTickets for help on using tickets.
Back to Top