Monday, July 31, 2017

Re: Group Permissions programmatically

Hello

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

Regards

Sent from my iPhone

On Jul 31, 2017, at 20:28, Александр Христюхин (roboslone) <roboslone@gmail.com> wrote:

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

On 31 Jul 2017, at 10:23, Frank Semaganga <semaganga@gmail.com> wrote:

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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/D062C2C9-125E-4DB0-85F6-E818CE6EB8F0%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment