#27406: Signals pre_delete and post_delete break logical relationship of models 
and
behave in diffrently with different backends
-------------------------------------+-------------------------------------
     Reporter:  Seti                 |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  1.10
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  Databases            |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Seti):

 Replying to [comment:3 Tim Graham]:
 > Resolved.

 If a field has attributes null=True and blank=True, and using signals
 pre_delete (as example) attribute "on_delete" does not working at all.
 I tried on_delete=models.CASCADE and models.SET_NULL, it is does not
 working.
 I made dived deep in SQL generate from the Django ORM.

 On my code (with activate signal "pre_delete")
 {{{
 User.objects.filter().delete()
 }}}

 it generated a next code (a main part):


 {{{
 DELETE FROM "users_user" WHERE "users_user"."id" IN ('87420c3a-
 dae9-4c87-9095-064d8806530b'::uuid)
 DELETE FROM "users_profile" WHERE "users_profile"."id" IN
 ('f4d4aa39-e645-4da0-aef3-c2853c5d5e91'::uuid)
 DELETE FROM "diaries_diary" WHERE "diaries_diary"."id" IN
 ('b12f5507-3957-4384-ab32-9ffbf30acd87'::uuid)
 INSERT INTO "notifications_notification" ("id", "level", "action",
 "is_read", "is_deleted", "is_public", "is_emailed", "created",
 "is_anonimuos", "user_id", "user_display_text", "target_content_type_id",
 "target_object_id", "target_display_text",
 "action_target_content_type_id", "action_target_object_id") VALUES
 ('1d2b02d2-aec0-45fd-a23f-ef01262f4960'::uuid, 'success', 'deleted_user',
 false, false, true, true, '2016-11-08T09:38:08.317519+00:00'::timestamptz,
 false, '87420c3a-dae9-4c87-9095-064d8806530b'::uuid, 'uкузнецов
 ([email protected])', NULL, NULL, NULL, NULL, NULL)
 UPDATE "notifications_notification" SET "level" = 'success', "action" =
 'deleted_user', "is_read" = false, "is_deleted" = false, "is_public" =
 true, "is_emailed" = true, "created" = NULL, "is_anonimuos" = false,
 "user_id" = '87420c3a-dae9-4c87-9095-064d8806530b'::uuid,
 "user_display_text" = 'uкузнецов ([email protected])'',
 "target_content_type_id" = NULL, "target_object_id" = NULL,
 "target_display_text" = NULL, "action_target_content_type_id" = NULL,
 "action_target_object_id" = NULL WHERE "notifications_notification"."id" =
 '1d2b02d2-aec0-45fd-a23f-ef01262f4960'::uuid
 }}}

 An error is here

 {{{
 "user_id" = '87420c3a-dae9-4c87-9095-064d8806530b'::uuid,
 "user_display_text" = 'uкузнецов ([email protected])''
 }}}

 **"user_id" must be NULL**, because a user must be deleted

 On a python side it is resolved very simple


 {{{
 class Notification(models.Model):

     ..........................

     def save(self, *args, **kwargs):
         ..................................
         user = kwargs.pop('user', None)
         if user is not None:

             # user will be deleted, so we keep it full name for display
             self.user_display_text = user.get_full_name()

             # manualy set field "user" to None (NULL in SQL), because the
 Django does not made it
             self.user = None

         self.full_clean()
         super(Notification, self).save(*args, **kwargs)
 }}}


 So, dear the Django developer team, please point this caution about using
 signals "pre_delete" and "post_delete" in the docs.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/071.ef053b16835b7ff9e68815232fd86458%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to