Get current user in model signal pre_save

1,004 views
Skip to first unread message

Xavier Palacín Ayuso

unread,
Sep 9, 2015, 11:13:19 AM9/9/15
to Django developers (Contributions to Django itself)
I want to collects current user in model signal pre_save, to prevent remove super user permission to current super users.
As I have now so I can not give administrative privileges to a normal user.

from django.db.models.signals import pre_delete, pre_save, post_save
from django.dispatch.dispatcher import receiver
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied


@receiver(pre_save, sender=User)
def save_user(sender, instance, **kwargs):
    
if instance._state.adding is True:
        
# we would need to create the object
        
print "Creating an object"
    
else:
        
#we are updating the object
        
if instance.is_superuser:
            
raise PermissionDenied

Tom Evans

unread,
Sep 9, 2015, 11:27:25 AM9/9/15
to django-d...@googlegroups.com
On Wed, Sep 9, 2015 at 4:11 PM, Xavier Palacín Ayuso
<xpal...@adncomunicacio.com> wrote:
> I want to collects current user in model signal pre_save, to prevent remove
> super user permission to current super users.

Hi Xavier. This mailing list is for discussing the development of
django itself, not for discussing how to develop in django.

For help with developing in django, please use django-users mailing
list (and be patient - just because no-one has answered you in 6 hours
does not mean you should kick it up to django-developers).

Cheers

Tom

Xavier Palacín Ayuso

unread,
Sep 10, 2015, 3:52:13 AM9/10/15
to Django developers (Contributions to Django itself)
Thanks
Reply all
Reply to author
Forward
0 new messages