# bulk\_update() example in docs

**URL:** <https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022>\
**Category:** Using the ORM\
**Created:** [November 14, 2022, 7:56pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022 "2022-11-14T19:56:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![biscotty666](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/biscotty666/32/10522_2.png) [@biscotty666](https://forum.djangoproject.com/u/biscotty666)\
**Post date:** [November 14, 2022, 7:56pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/1 "2022-11-14T19:56:43Z")

</div>

I’m trying to wrap my head around bulk\_update but am struggling to understand this in the docs:

```auto
>>> objs = [
... Entry.objects.create(headline='Entry 1'),
... Entry.objects.create(headline='Entry 2'),
... ]
>>> objs[0].headline = 'This is entry 1'
>>> objs[1].headline = 'This is entry 2'
>>> Entry.objects.bulk_update(objs, ['headline'])
2

```

Why are they using create() instead of update() in the list?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 14, 2022, 8:10pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/2 "2022-11-14T20:10:03Z")

</div>

Because the example is trying to demonstrate how `bulk_update` works.

The purpose of the `Entry.objects.create` is to create the initial entries in the database to allow `bulk_update` to function.

---

<div class="post-metadata">

**Author:** ![biscotty666](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/biscotty666/32/10522_2.png) [@biscotty666](https://forum.djangoproject.com/u/biscotty666)\
**Post date:** [November 14, 2022, 8:16pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/3 "2022-11-14T20:16:31Z")

</div>

So if I know the object already exists I would replace create() with get()?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 14, 2022, 8:20pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/4 "2022-11-14T20:20:45Z")

</div>

If you’re updating a set of items from a queryset, you’re probably better off using the [`update`](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#update) function.

If you already have a set of items loaded from a previous queryset and are doing other work with them, then `bulk_update` may be appropriate.

---

<div class="post-metadata">

**Author:** ![biscotty666](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/biscotty666/32/10522_2.png) [@biscotty666](https://forum.djangoproject.com/u/biscotty666)\
**Post date:** [November 14, 2022, 8:44pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/5 "2022-11-14T20:44:24Z")

</div>

I’ve got this working code and I’m trying to re-write it using bulk\_create and bulk\_update to boost performance (update is most important so I’m focusing on that first). This part of my code isn’t really too slow because there are only 50 players but I do need to address the subsequent import which involves importing 100+ characters for each player, which is very slow using the “traditional” way. So I want to understand this using this simpler case.

```auto
  for player in roster:
    new_player = player['id']
    if Player.objects.filter(playerId=new_player).exists():
      existing_player = Player.objects.get(playerId=new_player)
      existing_player.gp=player['gp']
      existing_player.name=player['name']
      existing_player.active=True
      existing_player.save()
      print('player updated')       
    else:
      new_player = Player(
        name=player['name'],
        playerId=player['id'],
        gp=player['gp'],
        active=True,
        updated=player['updated'],
      )
      new_player.save()
      print('New player saved')

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 14, 2022, 8:59pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/6 "2022-11-14T20:59:57Z")

</div>

If nothing else, you’re performing twice as many queries as necessary in your code when the player exists:

> [@biscotty666](#):
>
> ```auto
> if Player.objects.filter(playerId=new_player).exists():
> existing_player = Player.objects.get(playerId=new_player)
> 
> ```

Also, it may be worse than that depending upon what’s generating this `roster` iterable.

But yes, there may be some efficiencies to be gained by aggregating these new and modified `Player` objects in lists and using the `bulk` functions to batch those operations. In addition to reviewing the use of the [`bulk_`](https://docs.djangoproject.com/en/4.1/topics/db/optimization/#use-bulk-methods) functions, make sure you read about the caveats as well.

---

<div class="post-metadata">

**Author:** ![biscotty666](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/biscotty666/32/10522_2.png) [@biscotty666](https://forum.djangoproject.com/u/biscotty666)\
**Post date:** [November 14, 2022, 10:09pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/7 "2022-11-14T22:09:42Z")

</div>

Yeah I had noticed that but thanks for pointing it out. And “roster” is from an api call and is a json object.

---

<div class="post-metadata">

**Author:** ![biscotty666](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/biscotty666/32/10522_2.png) [@biscotty666](https://forum.djangoproject.com/u/biscotty666)\
**Post date:** [November 15, 2022, 3:58pm UTC](https://forum.djangoproject.com/t/bulk-update-example-in-docs/17022/8 "2022-11-15T15:58:28Z")

</div>

Just wanted to close out this thread:  
In the end I realized that what I was doing was wrong. Since this model represents the state of the api at the time of import and is never modified in the app it seemed more logical to delete all and then just create instead of updating since updating would implicate touching every record anyway.  
I really appreciate your input Ken.
