111

I have a model in django that I want to update only, that is, when I call it and set the data, it will not create a new record, only update the existing one. How can I do this? Here is what I have:

class TemperatureData(models.Model):   
    date = models.DateTimeField()   
    value = models.PositiveIntegerField()   
    alert = models.BooleanField()
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Ruben Quinones
  • 2,442
  • 8
  • 26
  • 30

5 Answers5

183

If you get a model instance from the database, then calling the save method will always update that instance. For example:

t = TemperatureData.objects.get(id=1)
t.value = 999  # change field
t.save() # this will update only

If your goal is prevent any INSERTs, then you can override the save method, test if the primary key exists and raise an exception. See the following for more detail:

pawas kr. singh
  • 416
  • 1
  • 4
  • 12
ars
  • 120,335
  • 23
  • 147
  • 134
  • 2
    isn't there another solution? This seems more like "SELECT / UPDATE" than simply "UPDATE" – Loic Aug 10 '15 at 08:57
  • How do you update all the fields of the record, with a new request that was send. Lets say the record with it's id was pulled into a form, one field was changed and it needs to be updated, do I have to run through every field to see if it changed? – Alfa Bravo Aug 22 '19 at 10:15
38

You should do it this way ideally

t = TemperatureData.objects.get(id=1)
t.value = 999
t.save(['value'])

This allow you to specify which column should be saved and rest are left as they currently are in database. (https://code.djangoproject.com/ticket/4102)!

Zaman Afzal
  • 2,009
  • 17
  • 23
  • 7
    I like this answer, but with the current version you need to use `t.save(update_fields=['value'])` to [force an update](https://docs.djangoproject.com/en/3.1/ref/models/instances/#specifying-which-fields-to-save). – user2540711yo Dec 21 '20 at 20:28
29

Sometimes it may be required to execute the update atomically that is using one update request to the database without reading it first.

Also get-set attribute-save may cause problems if such updates may be done concurrently or if you need to set the new value based on the old field value.

In such cases query expressions together with update may by useful:

TemperatureData.objects.filter(id=1).update(value=F('value') + 1)

To update multiple fields:

TemperatureData.objects.filter(id=1).update(
    value=F('value') + 1,
    another_value=F('another_value')+1
)
4

Django has some documentation about that on their website, see: Saving changes to objects. To summarize:

.. to save changes to an object that's already in the database, use save().

miku
  • 181,842
  • 47
  • 306
  • 310
4

In my scenario, I want to update the status of status based on his id

student_obj = StudentStatus.objects.get(student_id=101)
student_obj.status= 'Enrolled'
student_obj.save()

Or If you want the last id from Student_Info table you can use the following.

student_obj = StudentStatus.objects.get(student_id=StudentInfo.objects.last().id)
student_obj.status= 'Enrolled'
student_obj.save()
Viraj Wadate
  • 5,447
  • 1
  • 31
  • 29