【发布时间】:2020-03-29 16:27:13
【问题描述】:
这个名字很好用,但我可以弄清楚如何以同样的方式传递选项列表。那些字段是空白的。在调试中,选项显示正确设置。
forms.py
class MatchSheets(forms.Form):
""" Match sheets """
name = forms.CharField()
propertyuser = forms.ChoiceField(choices=(), required=False)
SheetSet = formset_factory(
MatchSheets,
extra=0
)
views.py
sheets = PropSheetNames.objects.filter(owner=request.user,
sponsoruser=sponsoru_id)
props = SponsorsUsers.objects.filter(owner=request.user,
id=sponsoru_id).all()
initial_set = []
choiceset = (((prop.id), (prop.alias)) for prop in props[0].properties_user.all())
for sh in sheets:
initial_set.append(
{'name': sh.name,
'propertyuser.choices': choiceset}
)
form = SheetSet(request.POST or None, initial=initial_set)
我知道有人会指出这可以用modelformset_factory 来完成整个事情,或者modelselect 用于propertyuser,但我遇到了这两个问题,只是手动操作给了我更多灵活性。
【问题讨论】:
标签: python-3.x django-forms django-views django-2.2