サイト全体で使用するフィールドの組み合わせに対して、次のカスタム検証があります。
class MyModelForm(forms.ModelForm):
my_field_type_constant = 'my_field_type'
my_field_code_constant = 'my_field_code'
class Meta:
model = MyModel
def validate_my_field_code(self, my_field_code_error, my_field_type, my_field_code):
validate = URLValidator(verify_exists=False)
try:
validate(my_field_code)
except ValidationError:
messsge = u"My custom error"
self._errors[my_field_code_error] = self.error_class([messsge])
def clean(self):
cleaned_data = super(MyModelForm, self).clean()
my_field_type = cleaned_data.get(self.my_field_type_constant)
my_field_code = cleaned_data.get(self.my_field_code_constant)
my_field_type_from_model = other_model.models.my_field_TYPES[1][0]
if my_field_type == my_field_type_from_model:
self.validate_my_field_code(self.my_field_code_constant, my_field_type, my_field_code)
return cleaned_data
サイトの多くのフォームで my_field_type と my_field_code の組み合わせを使用しています。DRYの原則を守りたい。これらのフィールドの検証を他のすべてのフォームにコピー アンド ペーストせずに、他のフォームで使用できるようにするにはどうすればよいですか?