from rest_framework import serializers from webservice_running.models import Sportsman from metaservicesynced.models import Client from SharixAdmin.models import SharixUser from webservice_running.exceptions import ClientNotDefined from rest_framework.response import Response class SportsmanSerializer(serializers.ModelSerializer): class Meta: model = Sportsman exclude = ["blocked_sportsman", "browse_sportsman", "saved_sportsman", "sport_preferrence", "achievement", "saved_event", "participant", "browse_event",] # User data phone_number = serializers.CharField(source="client.user.phone_number") first_name = serializers.CharField(source="client.user.first_name") last_name = serializers.CharField(source="client.user.last_name") email = serializers.CharField(source="client.user.email") # Client data requirements = serializers.CharField(source="client.user.requirements") status = serializers.CharField(source="client.user.status") ticket_status = serializers.IntegerField(source="client.ticket_status.pk") id_metaservice = serializers.IntegerField(source="client.id_metaservice") is_global = serializers.BooleanField(source="client.is_global") is_visible = serializers.BooleanField(source="client.is_visible") # def create(self, validated_data): # print("="*30) # print(validated_data['client'].status) # # # Temporary solution # # client_fields = ("requirements", "status", "ticket_status", "id_metaservice", "is_global", "is_visible") # # for attr, value in validated_data.items(): # # # If "client" in req, then User model is accessed # # if attr == "client": # # for u_attr, u_value in value.get('user').items(): # # setattr(user_instance, u_attr, u_value) # # else: # # setattr(instance, attr, value) # # for i in validated_data.items(): # # pass # # user_instance = SharixUser._default_manager.create(**validated_data) # # client_instance = Client._default_manager.create(**validated_data) # # instance = Sportsman._default_manager.create(**validated_data) # # return instance def update(self, instance, validated_data): client_instance = instance.client if not client_instance: raise ClientNotDefined() user_instance = instance.client.user for attr, value in validated_data.items(): # If "client" in req, then User model is accessed if attr == "client": for u_attr, u_value in value.get('user').items(): setattr(user_instance, u_attr, u_value) else: setattr(instance, attr, value) instance.save() user_instance.save() return instance ''' { 'client': { 'user': { 'phone_number': '99999999999', 'email': 'email' } }, 'bio': 'bio test'} '''