ModelSerializer doesn't enforce model constraints ? #8290
Answered
by
tomchristie
LiorA1
asked this question in
Potential Issue
-
Hi,
But I noticed another behavior using the code: class UrlMapper(models.Model):
...
def _generate_rand_str():
len_of_str = 5
res_str = "string_larger_then_6_characters"
return res_str
short_path = models.CharField(default=_generate_rand_str, max_length=6, unique=True)
class UrlMapperSerializer(serializers.ModelSerializer):
class Meta:
model = UrlMapper
fields = ['id', 'url', 'short_path', 'hits']
read_only_fields = ['id', 'short_path', 'hits'] The Unique constraint is enforced, but the max_len doesn't.. (a string larger then 6 characters can be inserted..) Didn't know if the problem is at Django or DRF or the docs, so opened here.. Is it something in the DRF source code ? |
Beta Was this translation helpful? Give feedback.
Answered by
tomchristie
Dec 10, 2021
Replies: 1 comment 1 reply
-
I wouldn't particularly expect defaults to be validated. 🤷♂️ |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
LiorA1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wouldn't particularly expect defaults to be validated. 🤷♂️
I assume it'll just us whatever
_generate_rand_str
returns there, regardless of any validation constraints.