Question

I want to create a group that newly registered users are assigned to with that lets the users create model instances but not edit or delete model instances.

I cannot find in the (django docs) : https://docs.djangoproject.com/en/1.5/topics/auth/default/

where I would add this code.

My guts says the group assignment should occur in the view that processes user registrations, but where should I initialize the group users are assigned to?

Was it helpful?

Solution

The group assignment can occur during a post_save signal on a User (i.e., after a user is created), or as you suggest, after a user registration form gets saved within a view. You can initialize the group right as you're assigning the user to the group, using get_or_create. For example:

def assign_group_to_newly_registered_users(sender, instance, **kwargs):
    group, created = Group.objects.get_or_create(name='Newly Registered Users')
    group.user_set.add(instance)

post_save.connect(assign_group_to_newly_registered_users, sender=User, dispatch_uid=__file__)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top