将初始化传递给Django的ModelFormSet [英] Passing initial into Django's ModelFormSet
问题描述
Django 1.4中的新增功能...如果在modelformset上设置初始化现在只适用于额外表单,而不适用于那些绑定到查询器实例的表单:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#providing-initial-values
所以上面的各种hack不再是必需的,使用initial是答案。
I have a model formset that I need to pass the keyword initial in to (to have some initial values in the formset, such as date set to today). As far as I can tell, the queryset argument (which defaults to YourModel.objects.all()) overrides this ability for initial empty forms to be created. I specified the queryset as: YourModel.objects.none(), which prevents it from filling in the formset with entries from the database, but I still can't figure out how to pass initial in effectively. Do I need to subclass a method from ModelForm?
New in Django 1.4 ...if you set initial on a modelformset it now only applies to the 'extra' forms, not those bound to instances from the queryset:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#providing-initial-values
So the various hacks above are no longer necessary and using initial is the answer.
这篇关于将初始化传递给Django的ModelFormSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!