How do I get a dropdown into a form whose options depend on the current user or another field?
I have a form with a dropdown that should only offer options belonging to the logged-in user's organisation. At the moment it offers every option in the table, which is both wrong and a data leak.
Defining the field on the form class works fine for a static list, but I need the options decided per request, and the form class does not know who is asking.
I have seen people build the field in the view and assign it, which works but feels wrong, and I have seen people filter it in the template, which definitely feels wrong.
What is the correct place for this?
@upgrade_notes_ute · 6h ago
Add a test for the rejection case, not just the display case.
The test everyone writes is "user A sees only their own options". The test that matters is "user A submits user B's option id directly and the form rejects it". Those are different code paths and only the second one is a security test.
It is three lines and it is the one that fails when somebody later moves the filtering into the view or the template for convenience.
Same thinking for the ordering: if a field is required, test that submitting it empty fails, because narrowing an option source to an empty set turns a required field into an impossible one, and that is a bug users report as "the page is broken".
Reply
Report