I have some validation in my modelserializer that doesn't work: here is my serializer class:
from rest_framework import serializers
from ..models import Status
class StatusSerializer(serializers.ModelSerializer):
class Meta:
model=Status
fields=[
'user',
'content',
'image',
]
def validate_content(self,value):
if len(value)>5:
raise serializers.ValidationError('Content is too long')
return value
def validate(self,data):
content=data.get('content',None)
if content=="":
content=None
image=data.get('image',None)
if content is None and image is None:
raise serializers.ValidationError('content or image is requiered')
return data
here is where I try to make an entry, and I expect the result to be False, but it comes up as True
data ={'user':1}
serializer=StatusSerializer(data=data)
serializer.is_valid()
True
data ={'user':1,'content':'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}
serializer=StatusSerializer(data=data)
serializer.is_valid()
True
here is my model just in case:
class Status(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
content = models.TextField(null=True, blank=True)
image = models.ImageField(upload_to=upload_update_image,null=True, blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now_add=True)
objects = UpdateManager()
def __str__(self):
return str(self.content)[:50]
class Meta:
verbose_name='Status post'
verbose_name_plural='Status posts'
app_label='status'
I appreciate if any one give me a hint of what I am doing wrong.
Comments
Post a Comment