Reference = models.CharField(unique=True, max_length=20) Time_raised = models.DateTimeField(default=timezone.now, editable=False) class CustomerReportRecord(models.Model): ExampleĪs an example of how REST framework uses explicit validation, we'll take a simple model class that has a field with a uniqueness constraint. If you want to drop down to using Serializer classes instead, then you need to define the validation rules explicitly. When you're using ModelSerializer all of this is handled automatically for you. There's no extra hidden validation behavior being called on the model instance. Printing the repr of a serializer instance will show you exactly what validation rules it applies.Any validation behavior being used for ModelSerializer is simple to replicate. It is easy to switch between using shortcut ModelSerializer classes and using explicit Serializer classes.It introduces a proper separation of concerns, making your code behavior more obvious.This is advantageous for the following reasons: With REST framework the validation is performed entirely on the serializer class. With ModelForm the validation is performed partially on the form, and partially on the model instance. Validation in Django REST framework serializers is handled a little differently to how validation works in Django's ModelForm class. This can be achieved by using validator functions and validator classes. However, sometimes you'll want to place your validation logic into reusable components, so that it can easily be reused throughout your codebase. Most of the time you're dealing with validation in REST framework you'll simply be relying on the default field validation, or writing explicit validation methods on serializer or field classes. Validators can be useful for re-using validation logic between different types of fields.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |