How do I get the default value for a field in a Django model?

I have a Django model with some fields that have default values specified. I am looking to grab the default value for one of these fields for us later on in my code. Is there an easy way to grab a particular field's default value from a model?


Solution 1:

TheModel._meta.get_field('the_field').get_default()

Solution 2:

As of Django 1.9.x you may use:

field = TheModel._meta.get_field('field_name')
default_value = field.get_default()

Solution 3:

You can get the field like this:

myfield = MyModel._meta.get_field_by_name('field_name')

and the default is just an attribute of the field:

myfield.default

Solution 4:

if you don't want to write the field name explicitly, you can also do this: MyModel._meta.get_field(MyModel.field.field_name).default