How can I programmatically obtain the max_length of a Django model field?

14,304

Solution 1

Person._meta.get_field('name').max_length will give you this value. But having to use _meta suggests this is something you shouldn't do in normal usage.

Edit: as Carl pointed out, this naming is misleading and it does seem quite acceptable to use it: http://www.b-list.org/weblog/2007/nov/04/working-models/

Read more at Django Docs: https://docs.djangoproject.com/en/dev/ref/models/meta/#django.db.models.options.Options.get_field

Solution 2

The question is regarding models, but for people trying to do the same for forms (that's how I ended up in this thread), I think this approach is quite simple and clear:
1. In a template:
{{form.name.field.max_length}}

2. In python code (e.g. in the view)
form.name.field.max_length

Share:
14,304

Related videos on Youtube

Mat
Author by

Mat

Updated on February 16, 2022

Comments

  • Mat
    Mat over 2 years

    Say I have a Django class something like this:

    class Person(models.Model):
        name = models.CharField(max_length=50)
        # ...
    

    How can I programatically obtain the max_length value for the name field?

  • Carl Meyer
    Carl Meyer over 14 years
    Good answer, but disagree with the second comment. The underscore in front of _meta is misleading, IMO (and I've seen Django core devs agree; it's partly there just to avoid name clashes with model fields). Using _meta isn't a bad smell, it's a good one; it means you're being DRY and not making unwarranted assumptions.
  • Ben James
    Ben James over 14 years
    Carl: Actually you do seem to agree with my comment, which was only observing the naming convention and the implication this would normally have. You are correct that it is misleading though: see b-list.org/weblog/2007/nov/04/working-models for example
  • Stephen Paulger
    Stephen Paulger over 14 years
    I've found this naming annoying in the past, because you can't access variables with underscores at the beginning in Django templates. We added a new property that pointed at _meta to get round that.
  • Furbeenator
    Furbeenator about 12 years
    Fantastic! I am using this to enforce the max length of form fields.
  • Nathan Tregillus
    Nathan Tregillus over 8 years
    unfortunately, the question was regarding models, not forms
  • ASKN
    ASKN over 6 years
    Is it a good practice to access a protected member from outside the class
  • Ginés Hidalgo
    Ginés Hidalgo about 6 years
    @NathanTregillus Yes, you are completely right, but that's how I ended on this post, looking how to do this for forms. So I assume other people will also end up here looking for the solution for forms. I have edited my answer to clarify this. Thanks.
  • pdoherty926
    pdoherty926 over 5 years
    This is how I found my way here, too. Thanks, @GinesHidalgo!
  • Rick Graves
    Rick Graves about 4 years
    I also found my way here because I was getting a form error -- the string was too long for the field. My code imports data via an API, and when doing that, "Two Scoops of Django" recommends using the form to validate the incoming data. To avoid the form error, before putting the data in the form, my code now checks the length of the incoming string against the max_length and cuts back strings that are too long. (No need to make the field long enough to accommodate excessively verbose incoming strings.)