This Rule is just not True.
Blank Field is only validation related, when you are using forms, it has nothing to do with database.
Delete this rule thanks.
Hello Jaime, and welcome to the Sonar community!
Indeed, allowing a field to be blank will change the behavior of form validation, and as a result, allow empty strings to be entered into the database as valid values. Having null=False will in addition eliminate the confusion between having null and “” represent the absence of a value.
While I understand that these concern two different components of the framework, we believe that avoiding using null=True and using blank=True instead remains the best practice. This is also the recommendation given in the Django documentation.
Acknowledging the possibility, however, that I could be missing some details, could you maybe provide me with an example of a use case where our suggestion could prove to be problematic?
Thank you in advance for the clarification.
Best,
Jeremi
He did not answer, so I add two points here
-
it is perfectly fine to save an optional string, if you intend to differentiate from no data to empty data internally. of course this alone would mean, you could simply exclude the rule (if sonarlint would support adhering to #noqa: S6553…). This is why the rule says “avoid”, and not “disallow”, which makes it questionable why this is a MAJOR code smell, by itself.
-
but the even harder case where null=True is still the correct solution, is when you add a new field to an existing model, where this is actually the recommended step, and then you run into sonar issues, that can be misleading. But rewriting a huge table is still less useful, than having two states of empty.
