Commit 50ed674
Fix User.__init__ mutating shared Field.validators list on every instantiation (#1374)
* Fix User.__init__ mutating shared Field.validators list on every instantiation
Declare UserUnicodeUsernameValidator on the User.username field at
class-body time instead of patching ``validators[0]`` inside
``User.__init__``. The old pattern mutated a field-level singleton list
on every ``User(...)`` call (ORM hydration, form save, queryset
materialisation, etc.), and the hard-coded index would silently
regress to Django's default validator if any third-party package or
future migration prepended its own validator.
Adds migration 0026 to update the stored field definition and
regression tests covering:
- the validator is declared on the field (no instance-level patching);
- usernames containing ``\``, ``|``, ``*`` still pass validation;
- 100 back-to-back instantiations do not grow the validators list;
- a third-party prepend does not clobber the OpenContracts validator.
Closes #1358
* Apply black/pyupgrade formatting to #1358 fix
- Make the module-level docstring in test_user_username_validator.py raw
so embedded ``\`` escape sequences don't trigger W605.
- Collapse the black-reformatted migration to the pyupgrade-canonical
single-line dict.
* Address review: fix help_text raw-string drift, tighten tests
* Address PR #1374 review: add username_validator class attribute
Django admin forms (UserChangeForm, UserCreationForm) reference
model.username_validator directly, separate from Field.validators.
Without this attribute, admins editing usernames containing \, |, *
would hit form-level rejection from the inherited
UnicodeUsernameValidator even though the field itself accepts them.
---------
Signed-off-by: JSIV <5049984+JSv4@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>1 parent fb62757 commit 50ed674
4 files changed
Lines changed: 129 additions & 5 deletions
File tree
- opencontractserver
- tests
- users
- migrations
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
73 | 92 | | |
74 | 93 | | |
75 | 94 | | |
| |||
147 | 166 | | |
148 | 167 | | |
149 | 168 | | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | 169 | | |
156 | 170 | | |
157 | 171 | | |
| |||
0 commit comments