Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set default group for new user in django?

how can I set default group for new users that I create? I didn't create custom model of user and My django version is 1.11.

like image 273
Ali Soltani Avatar asked Jan 31 '18 14:01

Ali Soltani


People also ask

How do I create a group in Django?

Django Admin Panel : In Admin Panel you will see Group in bold letter, Click on that and make 3-different group named level0, level1, level3 . Also, define the custom permissions according to the need. By Programmatically creating a group with permissions: Open python shell using python manage.py shell.

Should I use default Django user model?

Whenever you are starting a new Django project, always swap the default user model. Even if the default implementation fit all your needs. You can simply extend the AbstractUser and change a single configuration on the settings module.

How do I give permission to groups in Django?

With Django, you can create groups to class users and assign permissions to each group so when creating users, you can just assign the user to a group and, in turn, the user has all the permissions from that group. To create a group, you need the Group model from django. contrib. auth.


1 Answers

If you are not using custom user models, or proxy models, one possible option is to use signals, so whenever a user is created, you can assign the corresponding group:

from django.contrib.auth.models import User, Group
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        instance.groups.add(Group.objects.get(name='group_name'))
like image 197
Dalvtor Avatar answered Nov 01 '22 13:11

Dalvtor