Saving with update_fields - Deepstash
Metaverse

Learn more about artificialintelligence with this collection

Find out the challenges it poses

Learn about the potential impact on society

Understanding the concept of Metaverse

Metaverse

Discover 76 similar ideas in

It takes just

11 mins to read

Saving with update_fields

To prevent updating every field of the model you can use the update_fields parameter. It expects a list of fields that you want to save. The UPDATE that is run against the database then only updates those fields that you have specified.

For example, in case we only want to update the price of a toy, we would run "toy.save(update_fields=["price"])".

2

25 reads

MORE IDEAS ON THIS

Problems with save()

When updating an existing record, Django will update every field. That means, if you changed only one field on the model instance, all the fields will be updated (with the values staying the same). This is inefficient, especially if the model has many fields.

Another problem is that if ther...

2

20 reads

When to use save

  • when creating a new record.
  • you have a model instance already, and you need to change some field values.
  • you've overridden the save() method to include some custom model logic.
  • you defined pre_save and post_save signals for that model.

2

13 reads

Update

The ".update()" method is available on each queryset object of the ORM. For example, "Toy.objects.filter(price__lt=50).update(price=50)".

This would update the price of all toys that are below a certain price. While this is for a range of records, it could also be used to update a single re...

2

11 reads

When to use update

  • you are updating fields of one or many records (note: update cannot be used to create a new record, like save can).
  • you don't need to re-use a model instance after updating.
  • you don't need your overridden save() method or any pre_save or post_save signals to run.
  • you ...

2

12 reads

Save

The ".save()" method is used to write a model instance to the database. It can be an existing record or even a new one. For an existing record, Django will run a SQL UPDATE statement on the database. For a new record, Django will run an INSERT.

2

123 reads

CURATED FROM

Related collections

More like this

Key Principles Of OODA Loop

  • Gödel’s theorems: They indicate that any mental model that we try to work with will omit parts of information and we have to use Bayesian updating (Continuously update the confidence in your beliefs as you come across new information) to bring it in line with real...

Psychological hurdles that prevent people from saving money

Underlying many of our difficulties with saving is that we only pay attention to our most immediate needs rather than prioritising a long-term saving strategy. We may also have an impulse to keep purchasing items to show off, such as a new phone (even if your exi...

10 reasons to write a book

10 reasons to write a book

  1. It clarifies your thinking.
  2. It leaves behind a record of where you are in this moment.
  3. You can focus only on the people who want to hear from you and seek feedback from them.
  4. It’s a project that is completely and totally up to you.
  5. Because it’s a generous way ...

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving & library

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Personalized recommendations

Supercharge your mind with one idea per day

Enter your email and spend 1 minute every day to learn something new.

Email

I agree to receive email updates