Opened 4 months ago
Closed 4 months ago
#35726 closed New feature (wontfix)
db.models.TextField.formfield() does not behave like CharField.formfield() when null=True
Reported by: | Ole Laursen | Owned by: | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | empty_value |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have a specific use case where I need null instead of the empty string in the database when some text fields passing through a form are not filled in.
This works fine for CharFields, but I recently discovered that it doesn't for TextFields. For TextFields they always turn out as the empty string.
It looks like CharField has this specific support for setting empty_value = None in .formfield(), but TextField does not:
if self.null and not connection.features.interprets_empty_strings_as_nulls: defaults["empty_value"] = None
I would expect them to behave the same.
Change History (1)
comment:1 by , 4 months ago
Component: | Uncategorized → Forms |
---|---|
Keywords: | empty_value added |
Resolution: | → wontfix |
Status: | new → closed |
Type: | Uncategorized → New feature |
Version: | 5.0 → dev |
Note:
See TracTickets
for help on using tickets.
Hello Ole Laursen, thank you for taking the time to create this ticket. I understand your proposal and in fact I have created a small project that reproduces what you describe.
In order to provide a resolution for this ticket, I have made a lot of research, so I'll share my findings to then explain how I arrived to the final conclusion.
ModelForm
): Please note that bothCharField
andTextField
model fields map to aCharField
form field, they just have different configuration.empty_value
being automatically set toNone
forTextField
) can be easily achieved by adding this to your model form definition:empty_value
feature forCharField
was implemented when solving ticket #4136. I can't see an explicit conversation about supporting (or not) the new parameter forTextField
, which makes me think this was just missed in that work (due to the focus inCharField
).Because of all the above, at first I was inclined to accept this ticket, since I agree that this is definitely a useful mechanism to deal with the duality
None
/empty strings for these fields; and from my investigation there are no duplicate reports nor previous discussion about this. But, OTOH, making this change would mean effectively doing a backward incompatible change to the behavior ofTextField
when used in aModelForm
, and I don't see a way that we could provide a deprecation path to migrate code that would, at the same time, allow for the deprecation to be "solved" and stop alerting during the whole period of deprecation.Given the above, I think that the correct resolution for this ticket is
wontfix
, but if you disagree, I encourage you to propose and discuss this proposal with the community and gain consensus to make this change. To do that, please start a new conversation on the Django Forum, where you'll reach a broader audience and receive additional feedback.I'll close the ticket for now, but if the community agrees with the proposal, please return to this ticket and reference the forum discussion so we can re-open it. For more information, please refer to the documented guidelines for requesting features.