django处理多种形式

时间:2014-10-30 03:42:03

标签: django multiple-forms formsets

我的模特:

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

我想立刻保存这两张表格。 TimingForm包含1周的医生时间表,因此我需要7天的7个表格,并且每周最初应设置为星期日,星期一.... DoctorInfoForm包含有关医生的信息。

我尝试使用CreateView,但我需要在那里使用form_class?

我怎样才能成功?任何建议。

1 个答案:

答案 0 :(得分:0)

这在django中很简单。

if HospitalDoctorinfoform.is_valid() and Timingform.is_valid():
    #Dosomething
    HospitalDocinfoform.save()
    Timingform.save()

宾果!!