django mehrere Formulare mit Formularsätzen
Ich habe ein Modell:
class HospitalDoctor(models.Model):
hospital = models.ForeignKey(Hospital)
full_name = models.CharField(max_length=100, unique=True)
expertization = models.CharField(max_length=50)
nmc_no = models.CharField(max_length=20)
timings = models.ManyToManyField('Timing', related_name='shift_timing')
appointment = models.IntegerField(default=0)
def __unicode__(self):
return self.full_name
class Timing(models.Model):
hospital = models.ForeignKey(Hospital)
doctor = models.ForeignKey(HospitalDoctor)
day = models.CharField(max_length=20)
mng_start = models.IntegerField()
mng_end = models.IntegerField()
eve_start = models.IntegerField()
eve_end = models.IntegerField()
def __unicode__(self):
return self.day
und ich habe dafür ein Formular erstellt:
class HospitalDoctorInfoForm(forms.ModelForm):
class Meta:
model = HospitalDoctor
fields = ('hospital','full_name', 'ex,pertization', 'nmc_no')
class TimingForm(forms.ModelForm):
class Meta:
model = Timing
fields = ('day','mng_start', 'mng_end', 'eve_start', 'eve_end')
Hier möchte ich die Informationen über den Arzt wie seine persönlichen Daten von HospitalDoctorInfoForm und seinen einwöchigen Zeitplan von TimingForm erstellen.
Ich denke, ich sollte Formulare für das Timing in TimingForm für einen 7-Tage-Zeitplan mit dem Anfangswert des Tages wie Sonntag, Montag ... verwenden.
Ich habe Ansicht geschrieben:
class HospitalDoctorAddView(CreateView):
template_name = "hospital_doctor_add.html"
model = HospitalDoctor
def post(self, request, *args, **kwargs):
info_form = HospitalDoctorInfoForm(request.POST)
formset = modelformset_factory(request.POST, Timing, form=TimingForm, extra=7)
if formset.is_valid() and info_form.is_valid():
self.formset_save(formset)
self.info_form_save(info_form)
context['formset'] = formset
return render(request, self.template_name, context)
def formset_save(self, form):
frm = Timing()
frm.hospital = self.request.user
frm.mng_start = form.cleaned_data['mng_start']
frm.mng_end = form.cleaned_data['mng_end']
frm.eve_start = form.cleaned_data['eve_start']
frm.eve_end = form.cleaned_data['eve_end']
frm.save()
def info_form_save(self, form):
info = HospitalDoctor()
info.hospital = self.request.user
info.full_name = form.cleaned_data['full_name']
info.expertization = form.cleaned_data['expertization']
info.nmc_no = form.cleaned_data['nmc_no']
info.save()
Wenn ich dies tue, wird die Fehlermeldung "Erstellen einer ModelForm ohne das Attribut 'fields' oder das Attribut 'exclude' ist veraltet - Form TimingForm muss aktualisiert werden" ausgegeben. Ich brauche Hilfe. Ist es richtig, was ich tue, oder gibt es eine andere Möglichkeit, dies umzusetzen?