[Answer]-CBV How to set initial form data from model?

1👍

You can implement the method get_form_kwargs on your view, for example:

class YourView(FormView):
    #[...]
    def get_form_kwargs(self):
        kwargs = super(YourView, self).get_form_kwargs()
        kwargs['instance'] = Vlan.objects.get(pk=1)
        return kwargs

Or you can use an UpdateView and implement get_object

Edit: I think I misread your question, as you want to pre-fill the form; so passing instance= would not do what you want, as it would also save to that object. For using another object as a template, you could implement the get_initial method and return the fields of your object as a dict

👤sk1p

Leave a comment