ModelMultipleChoiceFiled和初始值

时间:2015-10-11 11:46:08

标签: django

我的表格:

class AdjustOffersForm(forms.Form):
    min_payout = forms.DecimalField()
    max_payout = forms.DecimalField()
    verticals = forms.ModelMultipleChoiceField(queryset=Vertical.objects.all())

查看:

offer_settings, created = OfferSettings.objects.get(id=id)
form = AdjustOffersForm(initial={
        'min_payout': offer_settings.min_payout,
        'max_payout': offer_settings.max_payout,
        'verticals': offer_settings.vertical,
    })

我没有在表单中获取初始值。如果我从表单中选择一些选项然后保存它,则信息将成功存储在数据库中。但是,如果我尝试恢复以前选择的值,我就无法获得它们。

实现这一目标的最佳方法是什么?感谢你们。

1 个答案:

答案 0 :(得分:0)

我明白了。

form = AdjustOffersForm(initial={
        'min_payout': offer_settings.min_payout,
        'max_payout': offer_settings.max_payout,
        'verticals': offer_settings.vertical.all().values_list('id', flat=True),
    })