DEV Community

Gahyun Son
Gahyun Son

Posted on • Edited on

I just tried to compare values between model and serializer

I encountered tests like below. And I want to know why he checked the value through serializer. So, I check the values.

  1. Create board
  2. Add param
  3. Check the param in the board
def test(self):
        board = create_board(user=self.user, title='Sunday Roast secret')
        com = Command.objects.create(user=self.user, content='So interesting')
        board.commands.add(com)

        params = {'commands': f'{com.id}'}
        res = self.client.get(BOARD_URL, params)

        b1 = BoardSerializer(board)
        self.assertIn(b1.data, res.data)
Enter fullscreen mode Exit fullscreen mode

board

Sunday Roast secret
Enter fullscreen mode Exit fullscreen mode

b1

BoardSerializer(<Board: Sunday Roast secret>): 
id = IntegerField(label='ID', read_only=True) 
title = CharField(max_length=255)
tags = TagSerializer(many=True, required=False):
id = IntegerField(label='ID', read_only=True)
name = CharField(max_length=255)
commands = CommandSerializer(...)
Enter fullscreen mode Exit fullscreen mode

Serializer returns all the values stored. If you want to check the value through the data included, you can use this way.

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more