Group Permissions programmatically

32 views
Skip to first unread message

Frank Semaganga

unread,
Jul 31, 2017, 3:23:28 AM7/31/17
to Django users
Hi!

I am having a challenge . My aim is to be able to assign some group permissions when the group is created. In admin.py I have a custom GroupAdmin class  on which I have the function save customized but the challenge is everything else get saved and reflected on admin page including user permissions(if i set permissions to the user direct) but the group permissions seems to be saved but when I go to admin page they are not assigned.

Can you share you experience !


        permits = Permission.objects.filter(content_type__app_label='myapp')

        ####Saved but not updated on Admin page
        obj.permissions=permits
        obj.save()


        #####Works Perfect and changes seen on admin page
        user = User.objects.create_user('username12', '', 'password')
        user.groups.add(obj)
        user.user_permissions=perms
        user.is_staff=True
        user.save()


obj is an object of Group.


Regards

"Александр Христюхин (roboslone)"

unread,
Jul 31, 2017, 7:30:34 AM7/31/17
to django...@googlegroups.com
Hi,

Why don't you use the same approach that is generally used with users? I mean you connect a signal to your User/Group model, that changes permission for created objects, like so:

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_user_permissions(instance=None, created=None, **kwargs) -> None:
    if created:
        instance.user_permissions.add(...)  # add actual Permission objects here

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f862d504-99fa-4a1f-a6ea-523ba4cca67a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Frank Semaganga

unread,
Jul 31, 2017, 7:58:09 AM7/31/17
to django...@googlegroups.com
Hello

Thank you for the suggestion! Let me adapt the approach and see.

Regards

Sent from my iPhone
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/E0dE_GOvl_0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages