'DRF models with unkown number of items to save in PostgreSQL

I am building an API that pulls data from Trello and posts them into our website. In order to save my boards, lists and cards, I made 3 different model classes with name and id both defined as Charfield, but I want to get the user's checklist, and the number of items are different for each user.

¿How to can I create a model that receives as many topics as my user wants to create?



Solution 1:[1]

First of all you have solutions in Django such like: VectorFields, JSONFields, etc. But I rarely recommend this kind of solutions.

What I use in my case is a relation of ManyToMany between this two classes.

Suppose this.

from django.db import models


class CheckList(models.Model):
   item_name = models.TextField()
   ...
   

class TrelloCard(models.Model):
   name = TextField()
   ...
   check_list = models.ManyToManyField(CheckList, null=True, blank=True, on_delete=models.PROTECT)

   ...

In JSON this relation will showed as:

{
 id:
 name:
 ...
 check_list: [
     id:
     item_name:
     ...   
 ]
}

Plus this solution will be perfect for working with the entire django-rest-framework, things such serialziers, filters, views, etc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 allexiusw