4👍
✅
Use validate() method on the serializer to check the length and raise ValidationError
if it doesn’t pass:
class YourSerializer(serializers.Serializer):
items = ItemSerializer(many=True)
def validate(self, attrs):
if len(attrs['items']) > YOUR_MAX:
raise serializers.ValidationError("Invalid number of items")
5👍
You can create a validate_items()
Django rest framework will display the error as a field error for that field. so parsing the response will be easier
class YourSerializer(serializers.Serializer):
items = ItemSerializer(many=True)
def validate_items(self, items):
if len(items) > YOUR_MAX:
raise serializers.ValidationError("Invalid number of items")
- [Django]-Apache + Django on Windows does not start
- [Django]-Django-CMS AppHooks with conflicting urls?
- [Django]-Cannot Connect to Django from outside the Local Server
- [Django]-How to modify django-ckeditor inline styles?
- [Django]-In Django, is there an easy way to render a text field as a template, in a template?
Source:stackexchange.com