class Foo(models.Model):
from = models.IntegerField()
to = models.IntegerField()
def clean(self): if self.from > self.to:
raise ValidationError("`From` must be before or equal to `to`")
class FooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
def validate(self, attrs):
model = self.Meta.model
instance = Model(attrs)
instance.clean()
def ordervalidator(dct):
if self.from > self.to:
raise ValidationError # Using Django ValidationError, because the model admin needs it?
class Foo(models.Model):
from = models.IntegerField()
to = models.IntegerField()
validators = [ordervalidator]
def clean(self):
for validator in self.validators:
validator(self.__dict__)
class FooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
validators = [ordervalidator]
class Foo(models.Model):
from = models.IntegerField()
to = models.IntegerField()
def clean(self): if self.from > self.to:
raise ValidationError("`From` must be before or equal to `to`")
def save(self, **kwargs):
self.full_clean()
super().save(**kwargs)
class ValidateOnSaveMixin: def save(self, *args, **kwargs): self.full_clean() super().save(*args, **kwargs) |
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/d4d5e79b-6aa5-4afe-8b45-4ada71ef5060o%40googlegroups.com.