manytomany and onetomany not save after createuser

Im so confused saving data with manytomany or onetomany using form django, i want to create user with associate Group and LocationModel, user is created but Group and LocationModel not save.

class Location(models.Model):
    name = models.CharField(max_length=50, null=True)
    admin = models.ManyToManyField(User, related_name="admin")
    updated_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-name"]

    def __str__(self):

class RegisterForm(UserCreationForm):
    group = forms.ModelChoiceField(
        label="Pilih Hak Akses",
                "class": "form-control",

    location = forms.ModelMultipleChoiceField(

    class Meta:
        model = User
        fields = ["username", "group", "location", "password1", "password2"]

def createUser(request):
    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
        form = RegisterForm()

    context = {
        "form": form,
    return render(request, "base/create-user.html", context)

Please help, im still newbie using python and django

Here RegisterForm uses User Model so if you create any instance it will be created for User not for Location Model.
Also for Location have ManyToMany Relation with User so when you create a Location instance and add users to that particular instance only then your Location instance will be created.
And in here Group refers to the Django’s default Group model used for user’s permissions and role? is it the same or are you creating a Group model by yourself.

1 Like

This is my form. i want to save Group Model that label as Pilih Hak Akses and Location Model label as Pilih Lokasi, how i can save both with UserForm ?

Yes, that group is refers from Django defauls Group model.

You can try using django signals to automatically set the one to one and many to many