我有一个使用自定义表单的 inline_formset。该表单具有我想调用的各种功能。但是,给定一个表单集的实例,我怎样才能访问那个(那些?)表单?
这是一些伪代码:
class MyModel(models.Model):
myField = models.ForeignKey(MyOtherModel)
class MyOtherModel(models.Model):
myField = models.CharField()
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
class MyOtherModelForm(forms.ModelForm):
class Meta:
model = MyOtherModel
def foo(self):
print "foo"
MyFormSet = inlineformset_factory(MyModel,MyOtherModel,formset=MyOtherForm)
def MyView(request):
myModel = MyModel()
myForm = MyModelForm(instance=myModel)
myFormSet = MyFormSet(intance=myModel)
# THIS FAILS...
myFormSet.foo()
# SO DOES THIS...
myFormSet.forms[0].foo()
# SO DOES THIS...
myFormSet.form.foo()
# ANY IDEAS?
return render_to_response('my_view.html', {"form" : myForm, "formset" : myFormSet },context_instance=RequestContext(request))