Austin
Austin

Reputation: 103

How to fill in a ModelForm within django view

I'm using a Django form that will be filled out multiple times by the same user. I would like to populate the form with data from previous submissions (I have a Foreignkey to the user that will be filling out the form), however, it is important that this is done server side as to not have the auto-populated data accessible user-side.

Is there a way to fill in specific fields that I don't want filled out again by the user within views.py before calling is_valid?

Views.py:

def home(request):
    if request.method == "POST":
        form = ptForm(request.POST)
        if form.is_valid():
            report = form.save(commit=False)
            report.pt_user = request.user
            report.save()
            form = ptForm()
        else:
            print(messages.error(request, "Error"))
            print(form.errors)
    else:
        form = ptForm()
    context = {'form': form,
    'ptz_data': pt_data.objects.all()
    }
    return render(request, 'home.html', context)

Upvotes: 0

Views: 2307

Answers (2)

Austin
Austin

Reputation: 103

Along with the advice Lewis gave me, another SO question was helpful and I came to the following answer. The trick is to request.POST.copy(), alter that variable, and then plug it back into your form (mine was form = ptForm(updated_request):

def home(request):
        if request.method == "POST":
            try:
                latest_entry = pt_data.objects.filter(pt_user=request.user).latest('id')
                updated_request = request.POST.copy()
                updated_request.update({
                'pt_sex': latest_entry.pt_sex,
                'pt_age': latest_entry.pt_age,})

                form = ptForm(updated_request)
            except:
                form = ptForm(request.POST)
            if form.is_valid():
                report = form.save(commit=False)
                report.pt_user = request.user
                report.save()
                form = ptForm()
            else:
                print(messages.error(request, "Error"))
                print(form.errors)

Upvotes: -1

Lewis
Lewis

Reputation: 2798

You can extract the latest entry via:

latest_entry = pt_data.objects.filter(pt_user=request.user).latest('id')

Method 1: Prefill data into form for user to see.

Then use the forms initial keyword to provide data into the form in your GET request. By filling it with the latest_entry attributes.

else:
   form = ptForm(initial={...})

Without context on what your form structure is I cannot provide the code for this but the usage on this can be found in the Django Docs

Method 2: Update values manually after form submission

if request.method == "POST":
    form = ptForm(request.POST)
    latest_entry = pt_data.objects.filter(pt_user=request.user).latest('id')
    if form.is_valid():
        report = form.save(commit=False)
        report.pt_user = request.user
        report.attr = latest_entry.attr
        report.save()
        form = ptForm()

Note: Replace .attr with the attributes you want to update from previous form entry.

Warning: Be careful when user has not filled out a form before. As the latest_entry will be empty.

Upvotes: 4

Related Questions