Django Rest Framework data display

Viewed 46

I have a model that contains a ManyToManyField like for instance:

class User(AbstractBaseUser, PermissionsMixin):
    login = models.CharField(unique=True)
    employee = models.ForeignKey(
        Employee,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
    )
    rooms = models.ManyToManyField(
        Room
    )

Now, I built a serializer that displays this data, but how can I change how the data looks like? Right now it looks like this:

{
        "employee_id": 25,
        "rooms": [
            {
                "location": "Berlin",
                "number": "25"
            },
            {
                "location": "Berlin",
                "number": "12"
            }
        ]
    }

and I would like it to look like this:

{
        "employee_id": 25,
        "Berlin": ["25", "12"]
    }

Any idea how could I achieve this?

0 Answers
Related