我想使用单一模型创建多个表单。有可能吗?
首先,我制作了一个包含我需要的所有字段的模型。 的 models.py
class AcademicsUpto10(models.Model):
PRE_PRIMARY_SUBJECT_CHOICES = (
('Abacus', 'Abacus'),
('Handwriting Basic', 'Handwriting Basic'),
('English Phonetics', 'English Phonetics'),
('English', 'English'),
('KG Academic Class', 'KG Academic Class'),
)
SUBJECT_1TO5_CHOICES = (
('All subjects', 'All subjects'),
('Vedic Maths', 'Vedic Maths'),
('Mathematics', 'Mathematics'),
('Science', 'Science'),
('English', 'English'),
('Hindi', 'Hindi'),
('Environmental Studies', 'Environmental Studies'),
('Mathematics - Science (Combo)', 'Mathematics - Science (Combo)'),
('Handwriting English/Hindi', 'Handwriting English/Hindi'),
)
SUBJECT_6TO10_CHOICES = (
('Mathematics', 'Mathematics'),
('Science', 'Science'),
('Computer Science', 'Computer Science'),
('English', 'English'),
('Social Science', 'Social Science'),
('Sanskrit', 'Sanskrit'),
('Environmental Studies', 'Environmental Studies'),
('French', 'French'),
('German', 'German'),
('Spanish', 'Spanish'),
('Mathematics - Science (Combo)', 'Mathematics - Science (Combo)'),
('Olympiad Maths/Science', 'Olympiad Maths/Science'),
)
BOARD_CHOICES = (
('CBSE', 'CBSE'),
('ICSE', 'ICSE'),
('State Board', 'State Board'),
('International Board', 'International Board'),
)
GENDER_CHOICES = (
('Male', 'Male'),
('Female', 'Female'),
('Flexible to get the best tutor', 'Flexible to get the best tutor'),
)
DAY_CHOICES = (
('Monday', 'Monday'),
('Tuesday', 'Tuesday'),
('Wednesday', 'Wednesday'),
('Thursday', 'Thursday'),
('Friday', 'Friday'),
('Saturday', 'Saturday'),
('Flexible on days to get the best tutor', 'Flexible on days to get the best tutor'),
)
SLOT_CHOICES = (
('Early Morning (6AM-8AM)', 'Early Morning (6AM-8AM)'),
('Morning (8AM-12AM)', 'Morning (8AM-12AM)'),
('Early Afternoon (12PM-3PM)', 'Early Afternoon (12PM-3PM)'),
('Late Afternoon (3PM-5PM)', 'Late Afternoon (3PM-5PM)'),
('Early Evening (5PM-7PM)', 'Early Evening (5PM-7PM)'),
('Late Evening (7PM-10PM)', 'Late Evening (7PM-10PM)'),
)
unique_id = models.UUIDField(default=uuid.uuid4, unique=True)
id = models.AutoField(primary_key=True)
created_at = models.DateTimeField(default=timezone.now)
ticket_size = models.CharField(max_length=30, default=1000, blank=True)
field = models.CharField(max_length=30, blank=True)
standard = models.CharField(max_length=30, blank=True)
pre_primary_subject = MultiSelectField(choices=PRE_PRIMARY_SUBJECT_CHOICES, default='Abacus', blank=False)
subject_1to5 = MultiSelectField(choices=SUBJECT_1TO5_CHOICES, default='All subjects', blank=False)
subject_6to10 = MultiSelectField(choices=SUBJECT_6TO10_CHOICES, default='Mathematics', blank=False)
subject_final = models.CharField(max_length=300, blank = True)
board = models.CharField(max_length=30, choices=BOARD_CHOICES, default='CBSE', blank=True)
day = MultiSelectField(choices=DAY_CHOICES, default='Flexible on days to get the best tutor', blank=False)
slot = MultiSelectField(choices=SLOT_CHOICES, default='Early Morning (6AM-8AM)', blank=False)
gender_preference = models.CharField(max_length=35, choices=GENDER_CHOICES, default='Flexible to get the best tutor', blank=False)
address = models.CharField(max_length=300, blank=True)
plot = models.CharField(max_length=150, blank=False)
street = models.CharField(max_length=150, blank=False)
landmark = models.CharField(max_length=150, blank=True)
name = models.CharField(max_length=150, blank=False)
contact = models.IntegerField(validators=[
MaxValueValidator(9999999999),
MinValueValidator(7000000000)],
blank=False)
email = models.EmailField(max_length=150, blank=False)
def __unicode__(self):
return (self.name)
然后我写了一些 forms.py
class AcademicsPrePrimaryForm(forms.ModelForm):
class Meta:
model = AcademicsUpto10
fields = [
'pre_primary_subject', 'day', 'slot', 'gender_preference',
'plot', 'street', 'landmark', 'name', 'contact', 'email'
]
widgets = {
'pre_primary_subject': forms.CheckboxSelectMultiple(attrs={'class': 'for-check-box'}),
'day': forms.CheckboxSelectMultiple(attrs={'class': 'for-check-box'}),
'slot': forms.CheckboxSelectMultiple(attrs={'class': 'for-checkbox'}),
'gender_preference': forms.RadioSelect(),
'plot': forms.TextInput(attrs={'placeholder': 'Plot Number', 'class':'for-field-before'}),
'street': forms.TextInput(attrs={'placeholder': 'Street', 'class': 'for-field-before'}),
'landmark': forms.TextInput(attrs={'placeholder': 'Landmark', 'class': 'for-field-before'}),
'name': forms.TextInput(attrs={'placeholder': 'Name', 'class': 'for-field-before'}),
'contact': forms.TextInput(attrs={'type': 'number', 'placeholder': 'Mobile Number', 'class':'for-field-before'}),
'email': forms.TextInput(attrs={'placeholder': 'Email ID', 'class': 'for-field-before'})
}
class Academics1to5Form(forms.ModelForm):
class Meta:
model = AcademicsUpto10
fields = [
'subject_1to5', 'board', 'day', 'slot', 'gender_preference',
'plot', 'street', 'landmark', 'name', 'contact', 'email'
]
widgets = {
'subject_1to5': forms.CheckboxSelectMultiple(attrs={'class': 'for-check-box'}),
'board': forms.RadioSelect(),
'day': forms.CheckboxSelectMultiple(attrs={'class': 'for-check-box'}),
'slot': forms.CheckboxSelectMultiple(attrs={'class': 'for-checkbox'}),
'gender_preference': forms.RadioSelect(),
'plot': forms.TextInput(attrs={'placeholder': 'Plot Number', 'class':'for-field-before'}),
'street': forms.TextInput(attrs={'placeholder': 'Street', 'class': 'for-field-before'}),
'landmark': forms.TextInput(attrs={'placeholder': 'Landmark', 'class': 'for-field-before'}),
'name': forms.TextInput(attrs={'placeholder': 'Name', 'class': 'for-field-before'}),
'contact': forms.TextInput(attrs={'type': 'number', 'placeholder': 'Mobile Number', 'class':'for-field-before'}),
'email': forms.TextInput(attrs={'placeholder': 'Email ID', 'class': 'for-field-before'})
}
class Academics6to10Form(forms.ModelForm):
class Meta:
model = AcademicsUpto10
fields = [
'subject_6to10', 'board', 'day', 'slot', 'gender_preference',
'plot', 'street', 'landmark', 'name', 'contact', 'email'
]
widgets = {
'subject_6to10': forms.CheckboxSelectMultiple(attrs={'class': 'for-check-box'}),
'board': forms.RadioSelect(),
'day': forms.CheckboxSelectMultiple(attrs={'class': 'for-check-box'}),
'slot': forms.CheckboxSelectMultiple(attrs={'class': 'for-checkbox'}),
'gender_preference': forms.RadioSelect(),
'plot': forms.TextInput(attrs={'placeholder': 'Plot Number', 'class':'for-field-before'}),
'street': forms.TextInput(attrs={'placeholder': 'Street', 'class': 'for-field-before'}),
'landmark': forms.TextInput(attrs={'placeholder': 'Landmark', 'class': 'for-field-before'}),
'name': forms.TextInput(attrs={'placeholder': 'Name', 'class': 'for-field-before'}),
'contact': forms.TextInput(attrs={'type': 'number', 'placeholder': 'Mobile Number', 'class':'for-field-before'}),
'email': forms.TextInput(attrs={'placeholder': 'Email ID', 'class': 'for-field-before'})
}
从这里开始,我想在同一个地方以三种形式保存主题(此主题将显示在管理面板中),我无法弄明白。
示例视图 - views.py
def standard8(request):
form = Academics6to10Form(request.POST or None)
if form.is_valid():
form.save(commit=False)
form.save(commit=False).address = request.POST['location']
form.save(commit=False).standard = 'Standard 8'
form.save(commit=False).field = 'Academics'
form.save()
message = "A new query has been added in " + str(form.standard)
message += "Name: " + str(form.name)
message += "Mobile Number: " + str(form.contact)
send_mail(emailSubject, message, emailFrom, emailList, fail_silently=False)
return render(request, 'thanks.html')
else:
return render(request, 'allForms/acadsstandard8.html', {'form': form})