我有两种模式: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