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 aManyToManyField
, but I suppose the solutions also can apply for that field as well, so I don’t mention it here.