[Answer]-Are the default values for the different Django model fields documented somewhere?

1đź‘Ť

âś…

I couldn’t find the answer in the documentation either, so I checked the source code for model fields. The “default default” for every model field except BinaryField is provided by this method:

def get_default(self):
    """
    Returns the default value for this field.
    """
    if self.has_default():
        if callable(self.default):
            return self.default()
        return force_text(self.default, strings_only=True)
    if (not self.empty_strings_allowed or (self.null and
               not connection.features.interprets_empty_strings_as_nulls)):
        return None
    return ""

Consequently, the “default default” for most field types is determined by how get_prep_value handles an empty string. The various implementations of get_prep_value can be found in the same source file. It looks like most fields don’t have a “default default”, because most implementations of get_prep_value don’t know what to do with an empty string. Notable exceptions to the rule are BooleanField (defaults to False), CharField (defaults to the empty string), and TextField (defaults to the empty string).

I hope this helps!

Leave a comment