Bind data to a form in template or view

1.3k views Asked by At

I have created a form as:

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

And here is the view to render the form:

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)

I want to bind the data from the 'owners' queryset to the 'ownersFormSet'. I am iterating over 'owners' in the template so can I bind the form data in each iteration in the template?

1

There are 1 answers

0
rsb On

i guess you need to use initial dictionary for your form, https://docs.djangoproject.com/en/1.10/ref/forms/api/#dynamic-initial-values