Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django serializer

# serialization in django

from rest_framework import serializers
from .models import Product

class ProductSerializers(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'
        
# adding extra fields, that are in models
    def to_representation(self, instance):
      data = super().to_representation(instance)
      data['is_on_sale'] = instance.is_on_sale()
      data['current_price'] = instance.current_price()
      return data
Comment

serialization in django

serialization in django

from rest_framework import serializers
from .models import Product

class ProductSerializers(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'
        
# Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. 
# Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
Comment

Django serializer,


class SnippetSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')

    def create(self, validated_data):
        """
        Create and return a new `Snippet` instance, given the validated data.
        """
        return Snippet.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
        Update and return an existing `Snippet` instance, given the validated data.
        """
        instance.title = validated_data.get('title', instance.title)
        instance.code = validated_data.get('code', instance.code)
        instance.linenos = validated_data.get('linenos', instance.linenos)
        instance.language = validated_data.get('language', instance.language)
        instance.style = validated_data.get('style', instance.style)
        instance.save()
        return instance
Comment

what is serializer in django

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks.
Serializers also provide deserialization, allowing parsed data to be converted back into complex types,
after first validating the incoming data.
Comment

PREVIOUS NEXT
Code Example
Python :: search in dict python 
Python :: soup itemprop 
Python :: negative number factor python 
Python :: what is *args and **kwargs in django 
Python :: get function in dictionary 
Python :: move file python os 
Python :: continue vs pass python 
Python :: root.iconbitmap 
Python :: how to change index in dataframe python 
Python :: fetch data from excel in python 
Python :: pandas replace last cell 
Python :: setattr python 
Python :: python numpy array 
Python :: raise a custom exception python 
Python :: check for double character in a string python 
Python :: creating new column with dictionary 
Python :: log loss python 
Python :: python split string into floats 
Python :: enumerate from 1 python 
Python :: dict get list of values 
Python :: pandas add value to excel column and save 
Python :: plot title overlapping yaxis python 
Python :: what is a slug 
Python :: how to create background images in tkinter 
Python :: depth first search python 
Python :: install chrome driver python 
Python :: list -1 python 
Python :: jupyter notebook not working 
Python :: python is program running 
Python :: python elapsed time module 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =