... にアクセスしたり、 full_clean() を直接呼び出す) がありますが、通常必要とはなりません。 一般的に、あらゆるクリーニング方法は ValidationError を投げる可能性があります。処理されるデータに問題がある場合、関連情報を ValidationError コンストラクタに渡します。 下記項目 ... に、 ValidationError を投げる際のベストプラクティスがあります。 ValidationError が投げられない場合、メソッドはクリーニングされた (標準化された) ... は再利用できるヘルパーです。バリデーション用の関数(または呼び出し可能オブジェクト)は、単一の引数を取り、無効な入力の場合に ValidationError を発生させます。バリデーション用の関数は、フィールドの to_python メソッドと validate メソッドが呼び出された後に実行されます ... ... 再利用するのに便利です。 たとえば、以下は偶数のみを許容するバリデータです。 from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ def validate_even ( value ): if value % 2 != 0 : raise ValidationEr ... た value を指定された正規表現で re.search() で検索します。デフォルトでは、一致が 見つからない 場合に message と code を持つ ValidationError を発生させます。 inverse_match を True に設定することで、動作を逆にできます。この場合、一致が 見つかった ときに Validation ... パイル済みの正規表現です。 デフォルトは空文字列で、すべての value から検索されます。 message バリデーションが失敗した場合に ValidationError で使用されるエラーメッセージです。デフォルトは "Enter a valid value" です。 code バリデーションが失敗した場合に Validation ... ... ts to Form error handling Form.add_error() Previously there were two main patterns for handling errors in forms: Raising a ValidationError from within certain functions (e.g. Field.clean() , Form.clean_<fieldname>() , or Form.clean() for non-field errors.) Fidd ... ate API. See 互いに依存するフィールドをクリーニングして検証する for an example using Form.add_error() . Error metadata The ValidationError constructor accepts metadata such as error code or params which are then available for interpolating into the error messag ... e (see ValidationError を発生させる for more details); however, before Django 1.7 those metadata were discarded as soon as the errors were added ... ... lForm documentation for more information. You should only need to call a model's full_clean() method if you plan to handle validation errors yourself, or if you have excluded fields from the ModelForm that require validation. Model. full_clean ( exclude = None ... validate_unique is True ), and Model.validate_constraints() (if validate_constraints is True ) in that order and raises a ValidationError that has a message_dict attribute containing errors from all four stages. オプションの exclude 引数を指定することで、検証と ... ant to run one-step model validation for your own manually created models. For example: from django.core.exceptions import ValidationError try : article . full_clean () except ValidationError as e : # Do something based on the errors contained in e.message_dict ... ... ( required = True ), size = 3 , remove_trailing_nulls = False ) [ "1" , "2" , "3" ] # -> [1, 2, 3] [ "1" , "2" , "" ] # -> ValidationError - third entry required. [ "1" , "" , "3" ] # -> ValidationError - second entry required. [ "" , "2" , "" ] # -> Validation ... , remove_trailing_nulls = True ) [ "1" , "2" , "3" ] # -> [1, 2, 3] [ "1" , "2" , "" ] # -> [1, 2] [ "1" , "" , "3" ] # -> ValidationError - second entry required. [ "" , "2" , "" ] # -> ValidationError - first entry required. SplitArrayField ( IntegerField ( r ... ... に一意な名前を指定する必要があります。 violation_error_code BaseConstraint. violation_error_code モデルのバリデーション 中に ValidationError が発生した場合に使用されるエラーコードです。デフォルトは None です。 violation_error_message BaseConstraint. violation_err ... or_message モデルのバリデーション の実行中に ValidationError が発生した場合に表示されるエラーメッセージです。デフォルトは "Constraint "%(name)s" is violated." です。 validate() BaseCo ... てクエリを実行します。制約を検証するために exclude リストのフィールドが必要な場合、制約は無視されます。 制約に違反した場合は ValidationError を発生させます。 このメソッドはサブクラスで実装する必要があります。 CheckConstraint class CheckConstraint ( * , condition ... ... その動作をより良く理解できます。各 Field インスタンスには、単一の引数を取る clean() メソッドがあり、 django.core.exceptions.ValidationError 例外を発生させるか、クリーンな値を返します。 >>> from django import forms >>> f = forms . EmailField () >>> f . clean ( "f ... oo@example.com" ) 'foo@example.com' >>> f . clean ( "invalid email address" ) Traceback (most recent call last): ... ValidationError : ['Enter a valid email address.'] field のコアとなる引数 各 Field クラスのコンストラクタは少なくとも以下の引数を受け付けま ... ォルトでは、各 Field クラスは値が必須であると想定しているため、空の値 ( None もしくは空文字列 ( "" )) を渡すと、 clean() は ValidationError 例外を発生させます。 >>> from django import forms >>> f = forms . CharField () >>> f . clean ( "foo" ) 'foo' >>> f . clean ... ... 最小限の長さであることを検証します。最小の長さは min_length パラメータでカスタマイズできます。 get_error_message () [ソース] ValidationError のエラーメッセージをカスタマイズするためのフックです。デフォルトでは "This password is too short. It must contain at least ... るパスワード以外は拒否され、1.0の値を指定すると、属性の値と同じパスワードのみが拒否されます。 get_error_message () [ソース] ValidationError のエラーメッセージをカスタマイズするためのフックです。デフォルトでは "The password is too similar to the <user_attribute>. ... のパスワードが含まれている必要があり、プレーンテキストまたはgzip圧縮されている必要があります。 get_error_message () [ソース] ValidationError のエラーメッセージをカスタマイズするためのフックです。デフォルトでは "This password is too common." (和訳: 「このパスワー ... ... eption FieldFetchBlocked [ソース] Raised when a field would be fetched on-demand and the FETCH_RAISE fetch mode is active. ValidationError exception ValidationError [ソース] ValidationError 例外は、データがフォームまたはモデルのフィールド検証に失敗すると発生しま ... ョン や モデルフィールドのバリデーション 、 バリデータのリファレンス を参照してください。 NON_FIELD_ERRORS NON_FIELD_ERRORS ValidationError でフォームやモデルの特定のフィールドに属さないものは、 NON_FIELD_ERRORS として分類されます。この定数は、他のフィールドをそ ... ... 用を持っている場合、その副作用は一度しか発生しないということを意味します。 Form.errors. as_data () フィールドとオリジナルの ValidationError インスタンスをマッピングする dict を返します。 >>> f . errors . as_data () {'subject': [ValidationError(['This field is re ... quired.'])], 'contact_email': [ValidationError(['Enter a valid email address.'])]} Use this method anytime you need to identify an error by its code . This enables thing ... stom format (e.g. XML); for instance, as_json() relies on as_data() . as_data() の必要性は、後方互換性に起因します。以前は ValidationError インスタンスは レンダリングされた エラーメッセージが Form.errors ディクショナリに追加されると同時に消失していました。できれ ... 2026年10月08日