I have the following ModelForm:
class IssueProcessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(IssueProcessForm, self).__init__(*args, **kwargs)
self.fields['number'].disabled = True
self.fields['title'].disabled = True
self.fields['body'].disabled = True
self.fields['body'].widget = forms.Textarea(
attrs={
'cols': 50
}
)
class Meta:
model = Issue
fields = (
'number', 'title', 'body', 'price'
)
I want to pre-populate the fields number
, title
, and body
with data from within a view, render the form, and make sure value corresponding to the fields show while also disabling these fields, so that user won't alter values. I want the price
field to be the only thing a user touches and when the save button is clicked, I want everything saved to the database. I tried the following:
def issue_process(request, repo_name, issue_number):
get_issue_number = request.session.get('issue_number_{}'.format(issue_number))
get_issue_title = request.session.get('issue_number_{}_title'.format(issue_number))
get_issue_body = request.session.get('issue_number_{}_body'.format(issue_number))
if request.method == 'POST':
form = IssueProcessForm(request.POST)
if form.is_valid():
issue = form.save(commit=False)
issue.number = get_issue_number
issue.title = get_issue_title
issue.body = get_issue_body
issue.save()
else:
form = IssueProcessForm(initial={
'number': get_issue_number,
'title': get_issue_title,
'body': get_issue_body
})
return render(request, 'core/issue_process.html', {'form': form})
...but each of the three fields keep saying this field is required
when I try submitting. What can I do please? Please note that get_issue_number
, get_issue_title
, get_issue_body
are the values I'd like to pre-populate the fields number
, title
, and body
with, respectively.