Can we define multiple models in the "Meta class" part of a Form ?
Here is my example:
from django import forms
from django.contrib.auth.models import User , Group
from django.forms import ModelForm
from django.utils.translation import ugettext as _
from profiles.models import Student , Tutor
class RegistrationForm(ModelForm):
email = forms.EmailField(label=_('Email Address:'))
password = form.CharField(label=_('Passsword:') , widget = forms.PasswordInput(render_value = False))
password1 = form.CharField(label=_('Verify Passsword:') , widget = forms.PasswordInput(render_value = False))
class Meta:
model = [Student , Tutor] ## IS THIS TRUE ???
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With