[Django] #26080: Missing field.on_delete

14 views
Skip to first unread message

Django

unread,
Jan 13, 2016, 7:06:52 AM1/13/16
to django-...@googlegroups.com
#26080: Missing field.on_delete
----------------------------------------------+--------------------
Reporter: srkunze | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
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') }}}.

--
Ticket URL: <https://code.djangoproject.com/ticket/26080>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 13, 2016, 7:33:00 AM1/13/16
to django-...@googlegroups.com
#26080: Missing field.on_delete
-------------------------------------+-------------------------------------
Reporter: srkunze | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Jan 13, 2016, 9:41:27 AM1/13/16
to django-...@googlegroups.com
#26080: Missing field.on_delete
-------------------------------------+-------------------------------------
Reporter: srkunze | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jan 13, 2016, 9:44:32 AM1/13/16
to django-...@googlegroups.com
#26080: Missing field.on_delete
-------------------------------------+-------------------------------------
Reporter: srkunze | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by srkunze):

Is {{{ForeignObjectRel}}} part of the official API?

--
Ticket URL: <https://code.djangoproject.com/ticket/26080#comment:3>

Django

unread,
Jan 13, 2016, 4:58:54 PM1/13/16
to django-...@googlegroups.com
#26080: Add field.on_delete to all relational fields
-------------------------------------+-------------------------------------
Reporter: srkunze | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Jan 28, 2016, 3:10:43 AM1/28/16
to django-...@googlegroups.com
#26080: Add field.on_delete to all relational fields
-------------------------------------+-------------------------------------
Reporter: srkunze | Owner: nobody

Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by TZanke):

* cc: tzanke@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/26080#comment:5>

Reply all
Reply to author
Forward
0 new messages