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})
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 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
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):
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:
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?
@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.
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?
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.