3 ms·
I really hate how Django doesn’t care about the difference between null (the lack of a value) and the empty string (the presence of a specific value). But if y
by JimDabell 6y ago
I really hate how Django doesn’t care about the difference between null (the lack of a value) and the empty string (the presence of a specific value). But if you’re going to use HTML forms as a way of editing that data in a generic way, I don’t really see a better option. How would you present a form containing a text field that was null in such a way that it would be preserved after the user edits a different field and saves?
- pmontra 6y agoYou should probably check the data coming from the form and what's in the database. If the form sends an empty string for a null field in the database, you keep the null. If it sends an empty string for a not null field, you set an empty string in the db field. This assumes that the semantic of null is "never touched before".
- tmpz22 6y agoYou use hidden fields in the html form to preserve previous values and then compare them in your input validation code server side. You set the default value of input type text and text area to the previous value. If the value was blank before and after you save as null, otherwise you set it to the new value.
- ovi256 6y agoIt cares very much about the difference, because it lets you set both separately.