“Co to jest serializator w Django” Kod odpowiedzi

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
Sleepy Sandpiper

Co to jest serializator w 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.
Aditya Coding

Serializator użytkownika w Django REST Framework

class Event(models.Model):
    user = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    date = models.DateTimeField(default=timezone.now)
    place = models.CharField(max_length=255)
Confused Cormorant

Odpowiedzi podobne do “Co to jest serializator w Django”

Pytania podobne do “Co to jest serializator w Django”

Więcej pokrewnych odpowiedzi na “Co to jest serializator w Django” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu