11π
No. But you donβt need to. Instead of instantiating and validating a single form, do it for each type of form you need to support.
# Define your model forms like you normally would
class StudentForm(ModelForm):
...
class TutorForm(ModelForm):
...
class RegistrationForm(Form):
email = ...
...
# Your (simplified) view:
...
context = {
'student_form': StudentForm(),
'tutor_form': TutorForm(),
'registration_form': RegistrationForm()
}
return render(request, 'app/registration.html', context)
# Your template
...
<form action="." method="post">
{{ student_form }}
{{ tutor_form }}
{{ registration_form }}
<input type="submit" value="Register">
</form>
If this means field names are duplicated across forms, use form prefixes to sort that out.
6π
No, it is not possible to define multiple models in the Meta class.
You can create a model form for each model, and then put multiple forms in the same html <form>
tag using the prefix argument.
Then in your view, you can check that each model forms is valid before saving.
Note that the django contrib.auth app comes with a UserCreationForm
(view source). You can probably re-use that instead of writing your own form.
- How to separate users (models) by admins and customers on Django
- How to parse URL encoded data recieved via POST
- Python + Django + VirtualEnv + Windows
- Mac pip install mysql-python unsuccessful
- What is the difference between the create and perform_create methods in Django rest-auth
0π
define your models in your form.py
form1
#using 1st model
form2
#using 2nd model
now edit your views.py in get method
args = {
"form1" = form1(),
"form2" = form2()
}
return render(request, "template_name", args)
now edit your template
<form .....>
form1.as_p
form2.as_p
...
</form>
- Django saving json value to database/model
- Django How to implement alert()(popup message) after complete method in view
- Django attribute error. 'module' object has no attribute 'rindex'