Foreign key relationships in django bulk_create request?

Can I use the bulk_create method for columns with foreign key relationships?

class Reports(models.Model): groupname=models.CharField(max_length=250, null=True, blank=True); datecreated = models.DateTimeField(null=True, blank=True); class Reportsquery(models.Model): group = models.ForeignKey(Reports,null=True, blank=True); queryset=models.CharField(max_length=1000, null=True, blank=True); list=[Reportsquery({"group__id":6,"queryset":"abc"}),....,...] Reportsquery.objects.bulk_create(list) 

a similar request works with the get_or_create () method, but returns an error when used with bulk_create () for example:

 Reportsquery.objects.get_or_create(group__id=6,quseryset="abc") 

In the above example, insert group__id = 6 into the Reportsquery table

+6
source share
1 answer

If what you are trying to do is create instances of Reports next to Reportsquery , then no bulk_create() will do this. However, if Reports instances already exist in the database, you can manually add their pk to the list that you pass to bulk_create() . Reportquery instances will Reportquery be created with the correct relationship to Reports .

+1
source

Source: https://habr.com/ru/post/970184/


All Articles