# Return id of bulk\_create() instance in Django with MySQL

**URL:** https://forum.djangoproject.com/t/return-id-of-bulk-create-instance-in-django-with-mysql/39372
**Category:** Using Django
**Created:** [March 8, 2025, 1:16am UTC](https://forum.djangoproject.com/t/return-id-of-bulk-create-instance-in-django-with-mysql/39372 "2025-03-08T01:16:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![slartdesigns](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/slartdesigns/32/27339_2.png) [@slartdesigns](https://forum.djangoproject.com/u/slartdesigns)
#### Post date: [March 8, 2025, 1:16am UTC](https://forum.djangoproject.com/t/return-id-of-bulk-create-instance-in-django-with-mysql/39372/1 "2025-03-08T01:16:10Z")

</div>

I was reading this topic here: [any plan to update bulk\_create to retrieve the ids for objects using MySQL?](https://forum.djangoproject.com/t/any-plan-to-update-bulk-create-to-retrieve-the-ids-for-objects-using-mysql/31251). 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:

```auto
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:

```auto
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:

```auto
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:

 ![educational plan](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/0/5/05528fc8465e9e2a0c752c9924bd7554d7d68b5d.png)

My models:

```auto
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.

---

<div class="post-metadata">

### Author: ![slartdesigns](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/slartdesigns/32/27339_2.png) [@slartdesigns](https://forum.djangoproject.com/u/slartdesigns)
#### Post date: [March 8, 2025, 5:37pm UTC](https://forum.djangoproject.com/t/return-id-of-bulk-create-instance-in-django-with-mysql/39372/2 "2025-03-08T17:37:06Z")

</div>

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.

```auto
# 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.
