#24391 closed Bug (fixed)
UUIDField with default=uuid4 triggers validation on otherwise empty inline formsets
Reported by: | Michael Angeletti | Owned by: | Tim Graham |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When using DateTimeField(default=timezone.now)
, inline model formsets know to avoid validation when no fields are changed, even though there is a value in that given DateTimeField
. However, when using another field (UUIDField
in my case) I'm getting inline form validation on otherwise empty forms, presumably due to the default UUID being different than the one generated during POST.
This is at least related to #24377, but since this isn't constrained to primary_key=True
cases, it needs its own ticket. Could a general solution to this render a specific solution to #24377 moot, or are they two separate issues?
Digression
How does this if user changed the default datetime value: count the form as having been changed
functionality work, btw? My understanding of dynamic default values like this is that there would be no way for Django to know whether the value has changed, unless the default value was included in a hidden field, or set in cache or cookies on page load... I'd love to document this, because if a user creates their own form field they might want to build in this sort of functionality.
Attachments (1)
Change History (9)
by , 10 years ago
Attachment: | validation-errors.png added |
---|
comment:1 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 10 years ago
The root cause is that the default is called twice when rendering the field so the hidden initial value is different from what appears in the input. I think this bug was hidden with the common case of datetime defaults because microseconds are truncated so usually the two values are equal, although it seems like it should trip across a second boundary at some point.
Maybe we can evaluate the default on the form field, rather than the widget. Backwards compatibility is the main concern.
comment:4 by , 10 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
Severity: | Normal → Release blocker |
comment:5 by , 10 years ago
#11390 - If you use a callable as default value on a model field, it gets called 3 times - is related and probably addressed at least partially by this.
comment:7 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Inline validation errors with a UUIDField