from django import forms from django.contrib.auth.models import User, Group from django.shortcuts import get_object_or_404 class SelectUserForm(forms.Form): pk = forms.ChoiceField( label="Utilisateur", help_text="Sera ajouté aux administrateurs du site.", validators=[], ) def get_user(self): pk = self.cleaned_data['pk'] return get_object_or_404(User, pk=pk) def populate(self): admins,_ = Group.objects.get_or_create(name='admins') choices = [(u.pk, u.first_name + ' ' + u.last_name + '(' + u.username + ')') for u in User.objects.all()] self.fields['pk'].choices = choices