Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.4 kB
4
Indexable
Never

# forms.py
class ContractUpdateForm(BaseModelForm):

    def __init__(self, *args, **kwargs):
        user = kwargs.pop("user", None)
        super().__init__(*args, **kwargs)

        self.fields[
            "contract_folder"
        ].queryset = ContractFolder.objects.get_folder_management_list(owner=user)
        self.fields["contract_folder"].empty_label = "(None)"
        self.fields["contract_folder"].to_field_name = "uid"        # Commenting this line out casusts the form to populate correctly when editing

        self.helper = FormHelper()
        self.helper.form_method = "post"

        self.helper.form_class = "form-horizontal"
        ### Snipped Code ###
        

# view.py

def view_contract_details_update_hx(request, uid):
    contract = Contract.objects.get_by_uid(uid)

    if request.POST:
        form = ContractUpdateForm(
            request.POST,
            user=request.user,
            instance=contract,
        )

        if form.is_valid():
            form.save()

            return redirect("contract:fragment_details", uid=uid, permanent=True)

    else:
        #### Issue occuring when this form is called
        form = ContractUpdateForm(
            instance=contract, user=request.user
        )

    context = {
        "contract": contract,
        "update_form": form,
    }

    return render(request, "contract/htmx_details_update.html", context)