Hacker News new | ask | show | jobs
by JimDabell 2040 days 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?
3 comments

It cares very much about the difference, because it lets you set both separately.
You 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".
You 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.