Django bulk_create function example

前端 未结 2 1747
半阙折子戏
半阙折子戏 2021-02-06 23:58

I\'m trying to understand bulk_create in Django

This was my original query I\'m trying to convert:

for e in q:
    msg = Message.objects.create(
                 


        
2条回答
  •  滥情空心
    2021-02-07 00:29

    The Official Example:

    class Entry(models.Model):
        blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
        headline = models.CharField(max_length=255)
        body_text = models.TextField()
        pub_date = models.DateField()
        mod_date = models.DateField()
    

    Now, to Bulk Create

    Entry.objects.bulk_create([
            Entry(headline='This is a test'),
            Entry(headline='This is only a test'),
     ])
    

提交回复
热议问题