Return id of bulk_create() instance in Django with MySQL

I was reading this topic here: any plan to update bulk_create to retrieve the ids for objects using MySQL?. And it looks like return the ID’s of a bulk_create() instance is theoretically possible, but I have pretty basic knowledge of Django, so really don’t know how can I solve my problem.

What I want to do is to have a dynamic form where user can add many fields they want and later save all that information in db on MySQL.

I have a table where I insert “indicators” values with bulk_create() method:

indicators_list = [EducationalPlanIndicators(indicators=i,points=j) for (i,j) in zip(request.POST.getlist('indicators'),request.POST.getlist('points'))]

indicators_inst = EducationalPlanIndicators.objects.bulk_create(indicators_list)

That table is related with a ManyToManyField with this other one:

plan = EducationalPlan.objects.create(
    date=request.POST.get('startdate'),
    themes=request.POST.get('theme'),
    strategy=request.POST.get('strategy'),
    nstrument=request.POST.get('instrument'),
    percent=request.POST.get('percent'),
    lapse=request.POST.get('lapse'),
    course_id=course,
    period_id=speriod,
    section_id=section,
    subject_id=subject,
    teacher_id=teacher,
    group_id=group.get(id=group_inst.id),
)
plan.save()

When EducationalPlan is saved, my reasoning is to use indicators_inst in a loop to add educationalplan_id and educationalplanindicators_id. Something like this:

indicators = EducationalPlanIndicators.objects.all()

for indicators_loop in request.POST.getlist('indicators'):
    if indicators_loop != ',':
        query = indicators.get(id=indicators_inst[0].id)
        plan.indicators_id.add(query)

But returns none. I know now why, but not how to approach it. Again, I’m pretty new to Django, so I’d appreciate some help and knowledge that can enlighten me or, maybe, tells a different approach, please :'c

Table with ManyToMany relationship:

My models:

class EducationalPlanIndicators(models.Model):
    id=models.AutoField(primary_key=True)
    indicators=models.CharField(max_length=255)
    points=models.PositiveSmallIntegerField(default=0)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now_add=True)

class EducationalPlan(models.Model):
    id=models.AutoField(primary_key=True)
    date=models.DateField()
    themes=models.TextField()
    strategy=models.CharField(max_length=255)
    instrument=models.CharField(max_length=255)
    percent=models.PositiveSmallIntegerField(default=0)
    lapse_data=((1,'Primero'),(2,'Segundo'),(3,'Tercero'))
    lapse=models.CharField(choices=lapse_data,max_length=7)
    group_id=models.ForeignKey(EducationalPlanGroups,on_delete=models.CASCADE)
    period_id=models.ForeignKey(SchoolPeriod,on_delete=models.CASCADE)
    criteria_id=models.ManyToManyField(EducationalPlanCriteria)
    indicators_id=models.ManyToManyField(EducationalPlanIndicators)
    course_id=models.ForeignKey(Courses,on_delete=models.PROTECT)
    section_id=models.ForeignKey(Sections,on_delete=models.PROTECT)
    teacher_id=models.ForeignKey(Teachers,on_delete=models.CASCADE)
    subject_id=models.ForeignKey(Subjects,on_delete=models.CASCADE)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now_add=True)

criteria_id field is a ManyToManyField, but I suppose the solutions also can apply for that field as well, so I don’t mention it here.

If somebody want to know a solution in the future, that’s the way I solve it:

There is currently no way to return the IDs from a bulk create insert with any built-in Django method or function. So what I did was use the filter() method.

I thought about filtering based on the values ​​given by the user, but one of the most obvious flaws is that these values ​​are not unique. However, because I store the creation date and time of each insert, it is possible to filter the most recent data by time. bulk_create() inserts all the data in a single query, so the time will always be the same for each query, no matter how long the list of inserted values ​​is.

# Filter objects using a DateTimeField with auto_now_add = True and storage the IDs in a list
indicators = EducationalPlanIndicators.objects.filter(created_at=indicators_inst[0].created_at).values_list('id', flat=True)

# Use a for loop to read the list and add the IDs one at the time
for idi in indicators:
    if idi != ',':
        query = EducationalPlanIndicators.objects.get(id=idi)
        plan.indicators_id.add(query)

I don’t know is this solution isn’t efficient for huge information insertions, but I think is good enough for simple insertions, like 8-10 rows.