0

我创建了一个表格:

class KeyForm(ModelForm):
    class Meta:
        model = Key
        fields = ['name', 'access_level', 'permission']

这是呈现表单的视图:

def key(request):
    template = 'keys.html'
    user = request.user
    key = get_object_or_404(Key, user=user)
    if key.access_level == "0":
        owners = Key.objects.filter(access_level = "0")
        ownersFormSet = formset_factory(KeyForm)
        context = {
            'owners': owners,
            'ownersFormSet': ownersFormSet,
        }
    return render(request, template, context)

我想将“所有者”查询集中的数据绑定到“所有者表单集”。我正在迭代模板中的“所有者”,所以我可以在模板的每次迭代中绑定表单数据吗?

4

1 回答 1

0

我想你需要为你的表单使用初始字典,https://docs.djangoproject.com/en/1.10/ref/forms/api/#dynamic-initial-values

于 2017-01-09T05:14:43.607 回答