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

前端 未结 2 1660
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 07:57

Say I have a Django class something like this:

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

How can I p

2条回答
  •  别那么骄傲
    2020-12-13 08:43

    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

提交回复
热议问题