I am trying to create an edit form for my model. I did not use a model form because depending on the model type, there are different forms that the user can use. (For example, one of the forms has a Tinymce widget, while the other doesn’t.)
Is there any way of setting the initial data of a form (not a ModelForm) using a model?
I tried the following but getting an error:
b = get_object_or_404(Business, user=request.user)
form = f(initial = b)
where f is a subclass of forms.Form
The error I am getting is AttributeError: 'Business' object has no attribute 'get'
The initial data needs to be a
dict(or at least have adict-like interface, which a Django model does not have).You can construct a
dictfrom your model usingdjango.forms.models.model_to_dict:This is the same function Django’s built in
ModelFormclass uses to set its initial data, and while you’ve specified that you don’t want to use aModelFormit may be more convenient to find some way of structuring your code that allows you to do so.