2022-06-24 13:42:16 +02:00
|
|
|
from rest_framework.serializers import ModelSerializer, SerializerMethodField
|
|
|
|
|
|
|
|
from room.serializers import RoomUserSerializer
|
2022-06-11 23:39:03 +02:00
|
|
|
from .models import CustomUser
|
|
|
|
|
2022-06-24 13:42:16 +02:00
|
|
|
|
2022-06-11 23:39:03 +02:00
|
|
|
class UserSerializer(ModelSerializer):
|
2022-06-24 13:42:16 +02:00
|
|
|
room = SerializerMethodField()
|
|
|
|
class Meta:
|
|
|
|
model = CustomUser
|
|
|
|
fields = ('email', 'last_login', 'date_joined', 'is_staff', 'username', "id_code", 'room', 'first_name', 'last_name')
|
|
|
|
|
|
|
|
def get_room(self, obj):
|
|
|
|
try:
|
|
|
|
return RoomUserSerializer(obj.room_set.all(), many=True, context={'userId': obj.id_code}).data
|
|
|
|
except:
|
|
|
|
return RoomUserSerializer(obj.room_set.all(), many=True).data
|
|
|
|
|
|
|
|
|
|
|
|
class UserEditSerializer(ModelSerializer):
|
2022-06-11 23:39:03 +02:00
|
|
|
class Meta:
|
|
|
|
model = CustomUser
|
2022-06-24 13:42:16 +02:00
|
|
|
fields = ('email', 'username', 'last_name', 'first_name')
|