13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Django charFieldでmin_lengthを設定する方法

Last updated at Posted at 2015-03-27

min_lengthはvalidatorを設定することによって使用できる。
ちなみに、validatorはFormとModelどちらにも設定できる。

###Modelでの使用

from django.db import models
from django.core.validators import MinLengthValidator

class MyModel(models.Model):
    name = models.CharField(max_length=20, blank=True, null=True,validators=[MinLengthValidator(6)])

###Formでの使用

from django import forms
from django.core.validators import MinLengthValidator

class MyModelForm(forms.Form):
	name = forms.CharField(validators=[MinLengthValidator(6)], max_length=20)

6文字以上になるように設定してます。

他にもvalidatorはいろいろ機能があったり、自分で作成したりもできるのようです。

参考

公式ドキュメント

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?