demo-backend/todos/serializers.py

22 lines
647 B
Python
Raw Normal View History

2025-01-16 14:20:20 +01:00
from rest_framework import serializers
from django.contrib.auth.models import User
from .models import Todo
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username')
extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
user = User.objects.create_user(
username=validated_data['username'],
password=validated_data['password']
)
return user
class TodoSerializer(serializers.ModelSerializer):
class Meta:
model = Todo
fields = ('id', 'title', 'completed', 'created_at')