Tuesday, June 18, 2013

Problem when creating a form for adding users to groups

Hi. 
I ran into a problem and have searched the group, in the official django and the internet and could not solve the problem.
I created a form that lists all the groups created and all user created and this adds form users within groups.
Already tried two ways and both give error


form.py

class alterar_usuario(forms.Form):
Usuario = forms.ModelMultipleChoiceField(queryset= User.objects.all() )
Grupo = forms.ModelMultipleChoiceField(queryset = Group.objects.all())


(way 1)
view.py

def cadastrarUsuarioGrupo(request):

    if request.method == "POST":   
        form = alterar_usuario(request.POST)
        
        if form.is_valid():
            Usuario = form.cleaned_data['Usuario']
            Grupo = form.cleaned_data['Grupo']

            usuarioOb = User.objects.get(id = Usuario)
            groupOb = Group.objects.get(id = Grupo)
            
            usuarioOb.groups.add(groupOb)

(way 2)
view.py

def cadastrarUsuarioGrupo(request):

    if request.method == "POST":   
        form = alterar_usuario(request.POST)
        
        if form.is_valid():
            Usuario = form.cleaned_data['Usuario']
            Grupo = form.cleaned_data['Grupo']
            usuarioOb = User.objects.filter(id = Usuario)
            groupOb = Group.objects.filter(id = Grupo)
            for user, group in usuarioOb, groupOb:
              user = usuarioOb.user
              group = grupoOb.groups
              usuer.groups.add(group)

Grateful.

--
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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate