[bug] Billing code page can't save a code with a client ("Select a valid choice")

Open Beginner friendly
#111 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
1-3 hours
Newbie friendliness
86/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
django, python
Domain
backend

Research direction

Read BillingCodeForm in jobtracker/forms.py and compare its client field widget with InlineBillingCodeForm, which the issue says works. Change the full-page field to use the single-choice widget described in the issue, then add and run a form test that posts a client. Done means the form accepts a valid client and the regression test passes.

Written by the indexing model from the issue text.

Description

Summary

On Operations → Billing Codes → Create (and edit), choosing a client and saving fails with "Select a valid choice. That choice is not one of the available choices." under Client. Codes can only be saved from that page without a client.

BillingCodeForm.client in jobtracker/forms.py is a single-choice forms.ModelChoiceField, but its widget is s2forms.ModelSelect2MultipleWidget. The multiple widget's value_from_datadict returns a list, which ModelChoiceField can't match against the queryset, so validation always fails when a client is chosen.

Reproduced in a Django shell:

BillingCodeForm(data=QueryDict(f"code=Training&client={client.pk}&region=GB")).errors
# {'client': ['Select a valid choice. That choice is not one of the available choices.']}

InlineBillingCodeForm, used for "create code" inside the Assign Billing Codes modal, uses ModelSelect2Widget and works. Its docstring notes the full-page form has a multiple client widget.

Suggested fix

class BillingCodeForm(forms.ModelForm):
    client = forms.ModelChoiceField(
        required=False,
        queryset=Client.objects.all(),
        widget=s2forms.ModelSelect2Widget(   # was ModelSelect2MultipleWidget
            ...

A form test that posts a client would catch a regression.

Workaround

Create the code from a job's Assign Billing Codes dialog, where the client defaults to the job's client, or in the Django admin.

Dominant language
HTML
Stars
6
Forks
4
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from brainthee/CHAOTICA

All issues in brainthee/CHAOTICA

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.