I'm trying to prepopulate a django FormView fields with data of another form (prepatory for a meeting minute creator). I made an project simplifiying and reproducing the error for better comprehension and easier solving... You see that the body field is present in the ModelForm, but not in the preparoty form. It is that, so I can dinamically create the minute with the data entered by the user in the preparatory form. So, basically, I must fill the "body" field with initial data, that will be dynamically created after using the info the user has just posted. I can do the dynamic thing, but I can't find a way to fill the field. The president and secretary fields are automatically filled by django FormView (class based view), but the body I cannot, even overriding the initial method as above.. By the way, if I print the "initial" in the get_initial method, it is empty, although the fields are filled up... Anyone has any idea on how to solve it?
I tried to override the get_initial() method, but it didn't work out.
The initial form (preparatory):
from django import forms
from core.models import MinuteModel
class MyForm(forms.Form):
president = forms.CharField(widget=forms.TextInput())
secretary = forms.CharField(widget=forms.TextInput())
The model:
class MinuteModel(models.Model):
president = models.CharField(max_length=120)
secretary = models.CharField(max_length=120)
body = models.CharField(max_length=1000)
The form to create the meeting minute:
class MyMinuteModelForm(forms.ModelForm):
class Meta:
model = MinuteModel
fields = "__all__"
The FormView:
class MyFormView(FormView):
form_class = MyMinuteModelForm
template_name = "core/creation.html"
def get_initial(self):
initial = super(MyFormView, self).get_initial()
initial['body'] = "asdasd"
return initial
I asked after days trying and now, right after I've answered I "solved" it. although not in the way I wanted. Now, I must initialize all the fields, so FormView became useless (so far as I needed it). It was nice because it prepopulated all data (except the fields that were not in the preparatory form, obviously, these I wanted to initialize with the view). But if I want to initialize one field, at least the way I did, it replaces all values for empty. There is the code:
So.. if you know any way to override only one field, it would be great to know.
If not, maybe in this case is better to use a simpler function based view.