简体   繁体   中英

How to pre-populate Django ModelForm fields

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.

When you disable a form field, the browser does not submit the field's value when you submit the form. Therefore you need to provide the initial data for POST requests as well as GET requests:

if request.method == 'POST':
    form = IssueProcessForm(
        request.POST,
        initial={
            'number': get_issue_number,
            'title': get_issue_title,
            'body': get_issue_body
        },
    )
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM