is there a way to assign users in groups from django views?

You can use ModelChoiceField to include select widget in your form. So please try this code:

from django.contrib.auth.models import Group
from django.forms import ModelForm, ModelChoiceField

from app.model import CustomUser

class UserForm(ModelForm):

    class Meta:
        model = CustomUser
        fields = [

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['group'] = ModelChoiceField(
            empty_label='No group'

Change CustomUserManager.create_user method in

from django.contrib.auth.models import Group

class CustomUserManager(BaseUserManager):

    def create_user(self, email: str, password: str, group: Group, **extra_fields):
        if not email:
            raise ValueError(_('The Email must be set'))

        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)

        if group is not None:

        return user

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top