Unexpected behaviour when calling serializer.create method, causes data loss in unrelated data (Django Rest Framework)

Django>=4.2.7,<4.3.0
djangorestframework>=3.14.0,<3.15.0
python-dateutil>=2.8.2,<2.9.0
psycopg2-binary>=2.9.9,<3.0.0
django-crontab>=0.7.1,<0.8.0

Im writing a tests for the serializer.create method. I prepared some data to pass it to the create method

    def create(self, validated_data):
        """
        Passing in the restructured validated_data to the
        CostumUserCreate method.
        """
        user_data = validated_data.get('user_data')
        profile_data = validated_data.get('profile_data')

        new_user = User.objects.create(
            user_data=user_data,
            profile_data=profile_data
        )

        return new_user

my test:

    def test_serializer_create(self):
        """
        Tests the correct restructuring of the validated data within
        the CustomUserSerializer create method.
        """
        data = {}
        user_data = {
            'email': 'updated.email@GMAIL.com',  # Not normalized
            'password': 'blabla123'
        }

        profile_data = {
            'first_name': 'Jay',
            'last_name': 'Miller',
            'position': self.position1
        }

        data['user_data'] = user_data
        data['profile_data'] = profile_data

        serializer = serializers.CustomUserSerializer()
        user = serializer.create(data)

        print(f'USER DATA: {user_data}')
        print(f'PROFILE DATA: {profile_data}')


when i try to print the user_data and profile_data now into the console it will be printed as an empty dictionary in the case of the user_data but not for the profile_data USER DATA: {} PROFILE DATA: {'first_name': 'Jay', 'last_name': 'Miller', 'position': <Position: Human Resource Specialist>}

but as soon as i comment out the serializer.create method the user_data appears again in the terminal USER DATA: {'email': 'updated.email@GMAIL.com', 'password': 'blabla123'} PROFILE DATA: {'first_name': 'Jay', 'last_name': 'Miller', 'position': <Position: Human Resource Specialist>}

i have no idea how this can even happen since the serializer.create method or therefore any method shouldnt be able to have any effect on the variables that are accessed before being passed into them...or did i understand something wrong here? i literally have no idaea how to fix this i tried to google it, chatgpt it but i still havent even a clue of whats happening here.

Back to Top