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 STASHED

Save vs Update in Django

helmut.dev

MORE IDEAS FROM THE ARTICLE

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 STASHED

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 there's a significant amount of time in between the fetching of the record and the saving of it, it is possible that another request will have updated one of the fields on that record, and your model instance still has the old value.

Calling ".save()" now will overwrite the new value that someone else saved.

2 STASHED

1 LIKE

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 record: "Toy.objects.filter(name="hippo").update(price=120)".

2 STASHED

  • 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 know what your new field values must be without having retrieved the record(s).

2 STASHED

  • 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 STASHED

Deepstash helps you become inspired, wiser and productive, through bite-sized ideas from the best articles, books and videos out there.

GET THE APP:

RELATED IDEAS

In general, a good checklist is:

  • Precise
  • Efficient
  • To the point
  • Easy to Use
  • Simple (just enough)

On the other hand, bad checklists are:

  • Vague
  • Imprecise
  • Long
  • Impractical
  • Too complex to use

94 STASHED

18 LIKES

How to Make Checklists: The Ultimate Tool to Save Time (With Examples)

link.medium.com

As already mentioned, debugging is considered a subset of troubleshooting. However, troubleshooting does not always entail solving the problem at that moment in time. There may be procedural constraints or workflow protocols that prevent the issue from being solved immediately. Debugging, on the other hand, is meant to discover and fix a problem all in the same session, whenever possible.

People use the two terms interchangeably, which can add to the confusion

9 STASHED

5 LIKES

Troubleshooting vs Debugging: What’s the Difference & Best Practices

stackify.com

It is a scam by which an internet user is deceived into revealing personal or confidential information which can be used illicitly.

At least 3.4 billion phishing emails are sent out worldwide every day.

Phishing scams account for half of all fraud attacks.

52 STASHED

2 LIKES

Why we fall for phishing emails - and how we can protect ourselves

ideas.ted.com