geriou-bir/grids/serializers.py

41 lines
1.1 KiB
Python
Raw Normal View History

2023-09-12 15:07:47 +00:00
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)
2023-09-13 20:53:44 +00:00
level_id = serializers.PrimaryKeyRelatedField(
write_only=True, source="level", queryset=Level.objects.all()
)
2023-09-12 15:07:47 +00:00
class Meta:
model = Word
2023-09-13 20:53:44 +00:00
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
2023-09-12 15:07:47 +00:00
class GridSerializer(serializers.ModelSerializer):
class Meta:
model = Grid
2023-09-19 15:56:27 +00:00
fields = ["id", "words", "width", "height", "words"]
2023-09-12 15:07:47 +00:00
class PlacementSerializer(serializers.ModelSerializer):
class Meta:
model = Placement
fields = ["id", "word", "grid", "coordinates_first_letter", "direction"]