[Fixed]-Django – How to get the max_length of a field

21👍

You can use the Model _meta API for accessing details of your model’s fields.

Here’s an example:

class MyModel(models.Model):
    title = models.CharField(max_length=200)

# retrieve the max_length
MyModel._meta.get_field('title').max_length

The _meta became a formal API in Django version 1.8. The documentation can be found at https://docs.djangoproject.com/en/stable/ref/models/meta/

Leave a comment