Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

python - How to set a default value for a form in every update?

I want to set a default value in a form and it will be reset as approval = False in every update. I tried something but it did not work it doesn't change. How can I fixed it?

forms.py

class UpdateDoaTableForm(forms.ModelForm):
    approval = forms.BooleanField(required=False,
                                  initial=False,
                                  label='Approved',
                                  widget=forms.HiddenInput()
                                  )

    class Meta:
        model = DoaTable
        fields = ('limit', 'approval')

views.py

def update_limit(request, id):
    limiting = get_object_or_404(DoaTable, id=id)
    form = UpdateDoaTableForm(request.POST or None, request.FILES or None, instance=limiting)
    limiting_item = DoaTable.objects.filter(id=id)

    if form.is_valid():
        form.save()
        return redirect('approvals:update_limit_list')

    context = {
        'form': form,
        'limiting_item': limiting_item,
    }

    return render(request, 'limitUpdate.html', context)

models.py

class DoaTable(models.Model):
    ...
    approval = models.BooleanField(default=False)
question from:https://stackoverflow.com/questions/66060766/how-to-set-a-default-value-for-a-form-in-every-update

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I edit my code and it is working now.:

views.py

if form.is_valid():
    update_form = form.save(commit=False)
    update_form.approval = False
    update_form.save()
    return redirect('approvals:update_limit_list')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...