geriou-bir/grids/serializers.py
2023-09-19 17:56:27 +02:00

41 lines
1.1 KiB
Python

from rest_framework import serializers
from .models import Word, Grid, Placement, Level
class LevelSerializer(serializers.ModelSerializer):
class Meta:
model = Level
fields = ["id", "level_number", "level_string"]
class WordSerializer(serializers.ModelSerializer):
level = LevelSerializer(many=False, read_only=True)
level_id = serializers.PrimaryKeyRelatedField(
write_only=True, source="level", queryset=Level.objects.all()
)
class Meta:
model = Word
fields = ["id", "word", "definition", "level", "level_id"]
# def create(self, validated_data):
# print(validated_data)
# level_data = validated_data.pop("level")
# level = Level.objects.get(pk=level_data)
# word = Word.objects.create(level=level, **validated_data)
# return word
class GridSerializer(serializers.ModelSerializer):
class Meta:
model = Grid
fields = ["id", "words", "width", "height", "words"]
class PlacementSerializer(serializers.ModelSerializer):
class Meta:
model = Placement
fields = ["id", "word", "grid", "coordinates_first_letter", "direction"]