84

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mat
  • 82,161
  • 34
  • 89
  • 109

2 Answers2

123

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

Rk..
  • 753
  • 1
  • 10
  • 27
Ben James
  • 121,135
  • 26
  • 193
  • 155
  • 11
    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. – Carl Meyer Dec 02 '09 at 02:30
  • 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 http://www.b-list.org/weblog/2007/nov/04/working-models/ for example – Ben James Dec 02 '09 at 09:44
  • 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. – Stephen Paulger Dec 02 '09 at 14:14
  • Fantastic! I am using this to enforce the max length of form fields. – Furbeenator Apr 13 '12 at 16:30
  • Is it a good practice to access a protected member from outside the class – ASKN Dec 06 '17 at 08:27
7

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

Ginés Hidalgo
  • 717
  • 9
  • 20
  • 4
    unfortunately, the question was regarding models, not forms – Nathan Tregillus Feb 04 '16 at 18:59
  • 1
    @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. – Ginés Hidalgo Jul 04 '18 at 16:05
  • 1
    This is how I found my way here, too. Thanks, @GinesHidalgo! – pdoherty926 Mar 21 '19 at 14:45
  • 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.) – Rick Graves May 03 '20 at 23:35