how to add methods to manuplate the model in serializers

Solutions on MaxInterview for how to add methods to manuplate the model in serializers by the best coders in the world

showing results for - "how to add methods to manuplate the model in serializers"
Mattia
02 Aug 2017
1from rest_framework import serializers, status
2from models import *
3
4
5class UserSerializer(serializers.ModelSerializer):
6    class Meta:
7        model = User
8        fields = ('username', 'first_name', 'last_name', 'email')
9
10
11class StudentSerializer(serializers.ModelSerializer):
12    """
13    A student serializer to return the student details
14    """
15    user = UserSerializer(required=True)
16
17    class Meta:
18        model = UnivStudent
19        fields = ('user', 'subject_major',)
20
21    def create(self, validated_data):
22        """
23        Overriding the default create method of the Model serializer.
24        :param validated_data: data containing all the details of student
25        :return: returns a successfully created student record
26        """
27        user_data = validated_data.pop('user')
28        user = UserSerializer.create(UserSerializer(), validated_data=user_data)
29        student, created = UnivStudent.objects.update_or_create(user=user,
30                            subject_major=validated_data.pop('subject_major'))
31        return student
Modesty
07 Jan 2017
1from django.db import models
2from django.contrib.auth.models import User
3
4class UnivStudent(models.Model):
5    """
6    A class based model for storing the records of a university student
7    Note: A OneToOne relation is established for each student with User model.
8    """
9    user = models.OneToOneField(User)
10    subject_major = models.CharField(name="subject_major", max_length=60)
queries leading to this page
how to retun email or username for foreign key instead of id in serializers in djangodjango modelserializercreate object 22id 22 3a 5b 22this field is required 22serialization update mothod overloading in djangodrf serializer create methodreturn none serializer method fielddjango serializer exclude in flattendrjango rest framework method on serialzierhow to return single filed from sreializer to views in djangoaccess serialized data djangomodel serializer import djangowhat is serializer in django rest frameworkhow to change a serializer validated dataadding tis null false in meta class serializerdjango serializer pass another serializer insidesending queryset in serialzerm ethod field drfaccess a serializer in a serializerfield 27id 27 expected a number but got error in serializing custom user model in django rest frameworkget the serializer id in the the create methodserializer validated data as jsonpassing in model serialization djangopost multiple id in serializer django createserializer method field if related object does not existrest framework alter data before serializingdjango serialiozersserialize model djangoserializer data pass variable to findprint serialized object djangohow to pass obj value from one serializer to anotherdrf modelserializer serialize custom field from jsonserializing django objects to json django serializer group by multiple objectsdjango serializer class metanested serializer django pass a listreturn saved object model from serializer djangodjango api manipulate databefore serialiserserializer validated data getimport serializer django modeldjango documentation serializermodel serializer django exampleserialized object data value change django serializerhow to change serializer attribute in django viewhow to get the no of objects in a model directly in django restframeworkhow to add methods to manuplate the model in serializers