[For reasons I mention 
in 
https://github.com/encode/django-rest-framework/issues/3144#issuecomment-431446248
 
I can't really just insert code into validate() in the ModelSerializer.]

My requirements are also not that complicated -- I only care about the 
validators passed to the field in Django when it is made, i.e., 

foo = models.CharField(validators=[....])

I also don't care about any foo_validate methods in the model that does 
further cleaning.

My desire is kind of rooted in that DRF doesn't seem to incorporate the 
max_length validator in the model's fields when a max_length is specified.  
 I also want to check regexes, which I have specified in the model to also 
create regex constraints.

So I've come up with two solutions ... I'm looking for feedback in case 
there is something I'm not thinking about.  

In say, FooSerializer,

foo = 
serializers.CharField(validators=FooModel._meta.get_field('foo').validators)

and the case when 'foo' is just in Meta.fields...

def validate_foo(self, value):
    if value is not None:
           for v in FooModel._meta.get_field("foo").validators:
                v(value)
    return value 

Is there a more elegant approach I should be using instead?




-- 
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-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/958cc50a-08b4-40ad-8490-1902f927ccd9n%40googlegroups.com.

Reply via email to