为什么Django inline_formset不会显示在CreateView中?

时间:2015-08-26 11:15:38

标签: python django inline-formset create-view

我有两种模式:TourLead和Languages。 TourLead有一个ForeignKey to Language。我需要一个CreateView来创建TourLead实例和语言(使用inline_formset)。创建的内联formset不会出现在页面上。我在模板中将其声明为{{lang_formset}}。

forms.py

class LeadForm(forms.ModelForm):
    ...

LanguageFormSet = inlineformset_factory(
    models.Language,
    models.TourLead,
    extra=1,
    exclude=[]
)

views.py

class LeadCreate(CreateView):
    template_name = 'leads/create-lead.html'
    success_url = reverse_lazy('leads-list')
    form_class = forms.LeadForm

    def get_context_data(self, **kwargs):
        context_data = super(LeadCreate, self).get_context_data(**kwargs)
        context_data['lang_formset'] = forms.LanguageFormSet()

        return context_data

models.py

class Language(models.Model):
    language = models.CharField(max_length=80,
                                verbose_name='Language')

    def __unicode__(self):
        return self.language


class TourLead(models.Model):
    name = models.CharField(max_length=80,
                            verbose_name='Name')
    gender = models.CharField(max_length=1,
                              verbose_name='Gender')
    language = models.ForeignKey('Language')
    card_number = models.CharField(max_length=15,
                                   verbose_name='Card Number',
                                   blank=True,
                                   null=True)
    expire_date = models.DateField(verbose_name='Expire Date',
                                   blank=True,
                                   null=True)
    professional = models.CharField(max_length=1,
                                    verbose_name='Professional')

    def __unicode__(self):
        return self.name

0 个答案:

没有答案