带有表单集的Django多个表单

问题描述 投票:1回答:1

我有一个模特:

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

并且我已经为此创建了表格:

class HospitalDoctorInfoForm(forms.ModelForm):

    class Meta:
        model = HospitalDoctor
        fields = ('hospital','full_name', 'expertization', 'nmc_no')

class TimingForm(forms.ModelForm):
    class Meta:
        model = Timing
        fields = ('day','mng_start', 'mng_end', 'eve_start', 'eve_end')

[在这里,我想创建有关医生的信息,例如HospitalDoctorInfoForm中的个人信息以及TimingForm中的一周工作时间表。

我认为我应该使用表单集在TimingForm中安排7天的时间安排,其初始值是星期天,星期一等。

我有书面看法:

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()

[执行此操作时,出现错误消息,“不建议使用不具有'fields'属性或'exclude'属性的ModelForm-表单TimingForm需要更新“。我需要帮助。我在做什么是对的,还是有另一种方法可以实现呢?

我有一个模型:HospitalDoctor(models.Model)类:医院= models.ForeignKey(医院)full_name = models.CharField(max_length = 100,unique = True)专业化= models.CharField(...

django forms formset multiple-forms
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.