# How do I show only my own group on the member add page?

**URL:** <https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799>\
**Category:** Using Django\
**Created:** [January 8, 2020, 2:47am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799 "2020-01-08T02:47:44Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 8, 2020, 2:47am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/1 "2020-01-08T02:47:44Z")

</div>

I’m using [django-groups-manager.](https://github.com/vittoriozamboni/django-groups-manager)

[Screenshot](https://i.ibb.co/zfHV1cg/group.jpg)

I’m already creating my group during registration. Then I make a group selection when adding members on the member insertion page. However, the entire group list appears. But I just want to see the group of person’s who add the user. So, it can include the added user in its own group and cannot access other groups. How can I do it?

**forms.py**

```
class CalisanForm(forms.ModelForm):
member = forms.CharField(max_length=100, label='Çalışan Adı')
CHOICES = [( group.name, group.name ) for group in Group.objects.filter()]
new_group_name = forms.ChoiceField(choices=CHOICES,widget = forms.widgets.RadioSelect())

class Meta:
    model = User
    fields = [
        
        'member',
        'new_group_name',
    ]

```

**views.py**

```
def calisan(request):
form = CalisanForm(request.POST or None)
if form.is_valid():
    member_name = form.cleaned_data['member']
    member = Member.objects.create(first_name=member_name)
    group = Group.objects.get(name=form.cleaned_data['new_group_name'])
    group.add_member(member)
    return redirect('home')
return render(request, 'accounts/calisan/calisan.html', {'form': form})
```

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 8, 2020, 2:58am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/2 "2020-01-08T02:58:04Z")

</div>

you have to thread it through the layers, passing the choices you want from the view to the form, example code below. It uses [ModelChoiceField](https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield) instead of ChoiceField because then you don’t have to create your own choices list.

I also don’t think you should be using ModelForm in this case - because you’re not using it to create a User. So you don’t need the `class Meta` on the form class either.

form

```auto
class CalisanForm(forms.Form):
    name = forms.CharField(max_length=100, label='Çalışan Adı')
    group = forms.ModelChoiceField(
        queryset=Groups.objects.all(),
        widget=forms.widgets.RadioSelect()
    )

    def __init__ (self, *args, **kwargs):
        qs = kwargs.pop("group_qs")
        super(). __init__ (*args, **kwargs)
        self.fields["group"].queryset = qs

```

view (function):

```auto
def calisan(request):
    queryset = Group.objects.filter(...)
    form = CalisanForm(request.POST or None, group_qs=queryset)
    if form.is_valid():
        name = form.cleaned_data['name']
        member = Member.objects.create(first_name=name)
        group = form.cleaned_data['group']
        group.add_member(member)
        return redirect('home')
    else:
        return render(request, 'accounts/calisan/calisan.html', {'form': form})

```

outline of how you do this using a class-based view:

```auto
class ClassView(...):
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["group_qs"] = Group.objects.filter(...)
        return kwargs

```

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 8, 2020, 3:24am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/3 "2020-01-08T03:24:57Z")

</div>

@takkaria I’m new to Django (…) What should it be?

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 8, 2020, 3:37am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/4 "2020-01-08T03:37:40Z")

</div>

From the information you’ve given I can’t tell. How do you know what groups the current user is in? Have you defined a link between a member and a user?

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 8, 2020, 3:47am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/5 "2020-01-08T03:47:03Z")

</div>

@takkaria When I register to my site, I create my group in the user registration form. I automatically join my group directly when I become a member. I’m logging in. Then, I want to add a new user. I want this user to be included in the group I created when I became a member. We need to be in the same group.

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 8, 2020, 4:01am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/6 "2020-01-08T04:01:27Z")

</div>

Sorry - I understand conceptually what’s going on but without the code I can’t help any further. What code would you write to show the current user’s Member object in a view? Do you have a custom user model or are you using a user profile to associate the member and the user together? Or do you have a custom Member class?

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 8, 2020, 4:16am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/7 "2020-01-08T04:16:15Z")

</div>

@takkaria There is no special connection between the user and the member. Both have the same status. What codes do you want (views.py codes?)

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 8, 2020, 4:22am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/8 "2020-01-08T04:22:42Z")

</div>

> There is no special connection between the user and the member. Both have the same status.

That’s your problem then - if there is no link between them how do you know which user record relates to which member?

> What codes do you want (views.py codes?)

The view for the user registration form would be useful I think.

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 8, 2020, 4:26am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/9 "2020-01-08T04:26:48Z")

</div>

**forms.py**

```
class RegisterForm(UserCreationForm):
username = forms.CharField(max_length=100, label='Kullanıcı Adı')
email = forms.EmailField(max_length=200, help_text='Required')
new_group_name = forms.CharField(max_length=100, label='Grup Adı')
password1 = forms.CharField(max_length=100, label='Parola', widget=forms.PasswordInput)
password2 = forms.CharField(max_length=100, label='Parola Doğrulama', widget=forms.PasswordInput)

class Meta:
    model = User
    fields = [
        'username',
        'email',
        'new_group_name',
        'password1',
        'password2',
    ]

def clean_password2(self):
    password1 = self.cleaned_data.get('password1')
    password2 = self.cleaned_data.get('password2')
    if password1 and password2 and password1 != password2:
        raise forms.ValidationError("Parolalar eşleşmiyor!")
    return password2

def clean_email(self):
    email = self.cleaned_data.get('email')
    lenghtw = len(User.objects.filter(email=email))
    if lenghtw > 0 :
        raise forms.ValidationError('Bu email adresine sahip bir kullanıcı zaten var.')
    return email

```

**views.py**

```
def register_view(request):
form = RegisterForm(request.POST or None)
if form.is_valid():
    user = form.save()
    new_group_name = form.cleaned_data['new_group_name']
    new_group, created = Group.objects.update_or_create(name=new_group_name)
    user.groups.add(AGroup.objects.get(name=new_group_name))
    user.save()
    password = form.cleaned_data.get('password1')
    new_user = authenticate(username=user.username, password=password)
    login(request, new_user)
    return redirect('home')
return render(request, 'accounts/form.html', {'form': form, 'title': 'Üye Ol'})
```

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 8, 2020, 4:51am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/10 "2020-01-08T04:51:23Z")

</div>

Ah OK, so it looks like members and users are kept in sync - do you have [`AUTH_MODELS_SYNC`](https://django-groups-manager.readthedocs.io/en/latest/auth_integration.html) turned on?

It looks like `queryset = request.user.groups` might get you what you want

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 8, 2020, 3:39pm UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/11 "2020-01-08T15:39:59Z")

</div>

@takkaria Thank you so much. Thanks to you, the problem is solved.

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 9, 2020, 2:17am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/12 "2020-01-09T02:17:56Z")

</div>

You’re welcome! ✨

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 9, 2020, 7:20pm UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/13 "2020-01-09T19:20:34Z")

</div>

May I ask one more thing? Can I also set the member’s password? How do I add a password section?

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 9, 2020, 7:30pm UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/14 "2020-01-09T19:30:13Z")

</div>

Django’s documentation has you covered here 🙂 take a look at the authentication system docs, in particular [changing passwords](https://docs.djangoproject.com/en/3.0/topics/auth/default/#changing-passwords).

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 9, 2020, 8:35pm UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/15 "2020-01-09T20:35:07Z")

</div>

I don’t want to change the password. I want to add password. 🥺 Can you show it in code?

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 9, 2020, 8:46pm UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/16 "2020-01-09T20:46:32Z")

</div>

I pointed you to that page because it shows a user object has `set_password()` function. Try it out 🙂

---

<div class="post-metadata">

**Author:** ![mufasa](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mufasa/32/516_2.png) [@mufasa](https://forum.djangoproject.com/u/mufasa)\
**Post date:** [January 11, 2020, 12:00am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/17 "2020-01-11T00:00:35Z")

</div>

I did that. It gives an error.

> # AttributeError at /accounts/calisan/calisan
> 
> ‘Member’ object has no attribute ‘set\_password’

**views.py**

```
def calisan(request):
    queryset = request.user.groups
    form = CalisanForm(request.POST or None, group_qs=queryset)
    if form.is_valid():
        name = form.cleaned_data['name']
        member = Member.objects.create(first_name=name)
        group = Group.objects.get(name=form.cleaned_data['group'])
        group.add_member(member)
        password = form.cleaned_data.get('password1')
        member.set_password(password)
        return redirect('home')
    else:
        return render(request, 'accounts/calisan/calisan.html', {'form': form})

```

**forms.py**

```
class CalisanForm(forms.Form):
name = forms.CharField(max_length=100, label='Çalışan Adı')
group = forms.ModelChoiceField(queryset=Group.objects.all(), widget=forms.widgets.RadioSelect(), empty_label=None)
password1 = forms.CharField(max_length=100, label='Parola', widget=forms.PasswordInput)
password2 = forms.CharField(max_length=100, label='Parola Doğrulama', widget=forms.PasswordInput)
def __init__ (self, *args, **kwargs):
    qs = kwargs.pop("group_qs")
    super(). __init__ (*args, **kwargs)
    self.fields["group"].queryset = qs

class Meta:
    model = User
    fields = [
        
        'name',
        'group',
        'password1',
        'password2',
    ]
    
def clean_password2(self):
    password1 = self.cleaned_data.get('password1')
    password2 = self.cleaned_data.get('password2')
    if password1 and password2 and password1 != password2:
        raise forms.ValidationError("Parolalar eşleşmiyor!")
    return password2

```

---

<div class="post-metadata">

**Author:** ![takkaria](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/takkaria/32/288_2.png) [@takkaria](https://forum.djangoproject.com/u/takkaria)\
**Post date:** [January 11, 2020, 12:51am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/18 "2020-01-11T00:51:08Z")

</div>

At the moment it looks like you’re creating a Member, and then the groups manager package is creating a user for you in through its synchronisation functionality. But Members don’t have a `set_password` method - Users do. So you’d have to create a User here to be able to set its password.

---

<div class="post-metadata">

**Author:** ![andrewgodwin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andrewgodwin/32/2_2.png) [@andrewgodwin](https://forum.djangoproject.com/u/andrewgodwin)\
**Post date:** [January 12, 2020, 12:31am UTC](https://forum.djangoproject.com/t/how-do-i-show-only-my-own-group-on-the-member-add-page/799/19 "2020-01-12T00:31:52Z")

</div>

(just a note - I moved this topic to Using Django as it’s not about working on Django code itself)
