[Python] Django serializer to convert list of dictionary keys to model fields

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024.

  1. Stack

    Stack Membro Participativo

    I am using pandas openpyxl to read excel file in my project. After reading the excel file I end up with list of dictionaries. These dictionaries have keys such as "Year 2024", "Range", "Main Point", and etc.

    I have a field in my Django app that has these fields such as "year", "range", "main_point".

    My question is how can I write a serializer to convert dictionary keys to fields in my model? I want to do this in serializer because I also want to validate dictionary data.

    So I have this list:

    my_data = [
    {"Year 2024": "5", "Range":"2", "Main Point": "OK"},
    {"Year 2024": "6", "Range":"3", "Main Point": "OK"},
    {"Year 2024": "7", "Range":"4", "Main Point": "OK"},
    ...
    ]


    and my model

    class MyModel(models.Model):
    year = models.IntegerField(...)
    range = models.IntegerField(...)
    main_pont = models.CharField(...)

    Continue reading...

Compartilhe esta Página