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') }}}.
--
Ticket URL: <https://code.djangoproject.com/ticket/26080>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/26080#comment:1>
Comment (by srkunze):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/26080#comment:2>
Comment (by srkunze):
Is {{{ForeignObjectRel}}} part of the official API?
--
Ticket URL: <https://code.djangoproject.com/ticket/26080#comment:3>
* status: new => closed
* resolution: => wontfix
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/26080#comment:4>
* cc: tzanke@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/26080#comment:5>