Santhosh
Santhosh

Reputation: 11774

Django: get_default of Charfield shows empty string

I have a model field of CharField/TextField

location = models.CharField(max_length=30, blank=True)

Now I check the default value in Django shell

[In}: User._meta.get_field('location').get_default()

[Out]: ''

I didn't mention any default value then how it's setting to ''?

Upvotes: 1

Views: 181

Answers (1)

Selcuk
Selcuk

Reputation: 59184

It returns an empty string if there is no explicit default value. From the source:

def get_default(self):
    """Return the default value for this field."""
    return self._get_default()

@cached_property
def _get_default(self):
    if self.has_default():
        if callable(self.default):
            return self.default
        return lambda: self.default

    if not self.empty_strings_allowed or self.null and not connection.features.interprets_empty_strings_as_nulls:
        return return_None
    return str  # return empty string

Upvotes: 1

Related Questions