V
V
VanBuren2021-02-07 17:38:14
Django
VanBuren, 2021-02-07 17:38:14

How to write a serializer?

Hello. I don't know much about drf. There are three models

# Create your models here.
class Years(models.Model):
    decadence = models.IntegerField(verbose_name="Десятилетие", primary_key=True)

    def __str__(self):
        """Возвращает строковое представление модели"""
        return str(self.decadence)


class Year(models.Model):
    year = models.IntegerField(verbose_name="Год выпуска", primary_key=True)
    photo = models.ImageField(verbose_name="Фотография года")
    decadence = models.ForeignKey(Years, on_delete=models.CASCADE)

    def __str__(self):
        """Возвращает строковое представление модели"""
        return str(self.year)


class Person(models.Model):
    name = models.CharField(max_length=50, verbose_name="Имя и фамилия")
    x_position = models.DecimalField(verbose_name="Отступ слева", default=0, decimal_places=5, max_digits=10)
    y_position = models.DecimalField(verbose_name="Отступ сверху", default=0, decimal_places=5, max_digits=10)
    year = models.ForeignKey(Year, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

You need to write a serializer that outputs information in this form
{
        "2010":  {
              "2011":  {
                    "photo": "img",
                    "persons": {
                         {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                        {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                     }
               },
              "2015":  {
                    "photo": "img",
                    "persons": {
                         {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                        {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                     }
               },
         },
         "2020":  {
              "2022": {
                    "photo": "img",
                    "persons": {
                         {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                        {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                     }
               },
             "2023":  {
                    "photo": "img",
                    "persons": {
                         {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                        {
                        "name": "имя",
                        "x_position": "12"
                        "y_position": "34
                        },
                     }
               },
         }
    }

In fact, you need to display all the tables by connecting by Foreign Key, investing in decades of the year, and years in persons of people.
I will be glad for any help. Or at least links to a similar situation

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question