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.
models.py
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):
return self.name
forms.py
class RegisterForm(UserCreationForm):
group = forms.ModelChoiceField(
label="Pilih Hak Akses",
queryset=Group.objects.all(),
required=False,
widget=forms.Select(
attrs={
"class": "form-control",
}
),
)
location = forms.ModelMultipleChoiceField(
queryset=Location.objects.all(),
required=True,
widget=forms.CheckboxSelectMultiple(),
)
class Meta:
model = User
fields = ["username", "group", "location", "password1", "password2"]
views.py
def createUser(request):
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
else:
form = RegisterForm()
context = {
"form": form,
}
return render(request, "base/create-user.html", context)
Please help, im still newbie using python and django