5

I would like to separate users into two different groups, employer or employee, at signup. I'm using django-userena and for the employer group I'm thinking of using a clone of the same signup view except with a different url tied to it.

So whoever signs up at url(r'^signup/employer/$) will be added to the employer group with

new user = user.groups.add(Group.objects.get(name=employer))

added to the view. Is this the right approach?

ekad
  • 14,436
  • 26
  • 44
  • 46
Jesramz
  • 97
  • 1
  • 7

1 Answers1

8

Edited: form.save() returns the user just created. You have then to simply add it to your group. Your view should look something like:

form = signup_form() 
if request.method == 'POST': 
    form = signup_form(request.POST, request.FILES) 
    if form.is_valid(): 
        user = form.save()
        user.groups.add(Group.objects.get(name='employer'))

I would also consider using signals, if what you want to do is to add every user to your employer group. Something like this will add each newly created user to it, and will allow you to use the default signup view from userena:

# somewhere, in your models.py file
@receiver(post_save, sender=User, dispatch_uid='myproject.myapp.models.user_post_save_handler')
def user_post_save(sender, instance, created, **kwargs):
    """ This method is executed whenever an user object is saved                                                                                     
    """
    if created:
        instance.groups.add(Group.objects.get(name='employer'))
StefanoP
  • 3,798
  • 2
  • 19
  • 26
  • Currently the view has: `form = signup_form() if request.method == 'POST': form = signup_form(request.POST, request.FILES) if form.is_valid(): user = form.save() ` The link to the view [link]https://github.com/bread-and-pepper/django-userena/blob/master/userena/views.py [link] – Jesramz Jan 21 '12 at 00:53
  • Maybe right before `user = form.save()` on the view, I can add `new_user.groups.add(Group.objects.get(name='employer'))` and I think this is all that is needed in the new view? Because the view calls a form that has a save method that creates the user already. [userena_forms](https://github.com/bread-and-pepper/django-userena/blob/master/userena/forms.py) – Jesramz Jan 21 '12 at 01:34
  • 1
    thanks for helping me. I did this in my app.forms `class SignupFormExtra(SignupForm): def save(self): new_user = super(SignupFormExtra, self).save() new_user.save() new_user.groups.add(Group.objects.get(name='employer')) return new_user` and added this to urls.py `url(r'^accounts/signup/employer$', 'userena.views.signup', {'signup_form': SignupFormExtra}),` at accounts/signup adds me to the employer group. I only want to be in the employer group at accounts/signup/employer – Jesramz Jan 21 '12 at 23:32
  • okay that makes sense. Then your solution should work, just add an URL on 'accounts/signup/employer' with your custom form as you did, and another URL on 'accounts/signup' with the default form for other users (pay attention on security issues, though...) your custom form works, just remove the `new_user.save()` since it's been already done by the superclass. – StefanoP Jan 22 '12 at 20:38