How to pass arguments when deleting an object? ex: object.delete(deleted_by=someone)

17 views
Skip to first unread message

Neto

unread,
May 13, 2015, 7:36:02 PM5/13/15
to django...@googlegroups.com
I want to pass an argument to delete an object, that argument will be handled by the signal.

Car.objects.get(pk=1).delete(deleted_by=someone)

models:

@receiver(post_delete, sender=Car)
def ref_person(sender, instance, **kwargs):
    who_deleted = ?


How do I get this argument?

Stephen J. Butler

unread,
May 13, 2015, 8:15:27 PM5/13/15
to django...@googlegroups.com
Model.delete doesn't take any arguments other than "using". If you
want to pass more context, you'll have to override Model.delete in
your project, create your own signal, and then pass the extra
arguments to it. Same with QuerySet.delete if you want to override
mass deletion also.

Or, even though people on here hate this, you can also store the user
in a thread local variable. For example, using this module (untested,
but theoretically correct):
https://gist.github.com/sbutler/5157265aa97bba665349

with setctxt(deleted_by=someone):
Car.objects.get(pk=1).delete()

@receiver(post_delete, sender=Car)
def ref_person(sender, instance, **kwargs):
who_deleted = getctxt('deleted_by', None)
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3ed8e143-7dbd-4de1-ad6f-3b54e12cefca%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages