How do I access the max_length
of a model field in Django? I am writing a serializer with the REST framework and I want to write a custom validation function to validate the length. Note that i am using SQLite, so the database itself does not enforce the max_length
.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With