我有一个基于非常简单的模型表单的inlineformset。我想要一种方法来将一个choiceset添加到表单的某个字段,但我想从views.py文件中执行它,因为choiceset是动态生成的。这可能吗? Python2.7,Django1.5
models.py
class RoleForm(ModelForm):
class Meta:
model = Role
fields = ['role_type', 'user', 'item']
views.py
def new(request):
''' Create a new application '''
user = request.user
associatedRoleTypes = getAssociatedRoleTypes(Application)
#^^^this returns a queryset, and is what I want model_choices to be equal to
RoleFormSetFactory = inlineformset_factory(Application, Role, can_delete=False, form=RoleForm)
if request.method == 'POST':
applicationForm = ApplicationForm(request.POST)
if applicationForm.is_valid():
new_application = applicationForm.save()
roleInlineFormSet = RoleFormSetFactory(request.POST, instance = new_application)
if roleInlineFormSet.is_valid():
roleInlineFormSet.save()
else:
print roleInlineFormSet.errors
return redirect(reverse('index'))
else:
applicationForm = ApplicationForm()
roleInlineFormSet = RoleFormSetFactory()
return render(request, 'who/editapp.html',
{'roleInlineFormSet': roleInlineFormSet,
'applicationForm': applicationForm
})