Reputation: 227
I have an entry already in my database table and i want to update it with the values 12
and 6
. But for some reason the update function doesn't seem to be doing anything. I dont want to create a new instance, i just want to write over the value that are already in there. PF
is the name of my DB table. I understand that the objects
links both to the pass_number
and fail_number
attributes of the table model, so i presume that both would be updated with the values. However when I go into the table i still see the old values.
event1 = PF(
pass_number = 12,
fail_number = 6,
)
event1.objects.update(event1)
The error i see in the terminal is:
TypeError: update() takes 1 positional argument but 2 were given
Upvotes: 0
Views: 1580
Reputation: 475
If PF
is your model name and you want to update a record in database.
First of all you have to fetch the record from database that needs to be updated. Then you can update that record.
To fetch single record you can use get
method and then update relevant fields and save the single record. It will update your existing record.
Example Code to update single record in database
event1 = PF.objects.get(pk=1)
event1.pass_number = 12
event1.fail_number = 6
event1.save()
NOTE: Here please replace 1
with the primary key of your record that exists in database.
To update multiple records you have to fetch all the records that needs to be updated. You can use filter
to filter the data or can use all
if you want to fetch all the records.
Example Code to update all records in database
events = PF.objects.all()
events.update(pass_number = 12, fail_number = 6)
Example Code to update filtered record in database
You can see examples with filter and update on following link. Thanks https://docs.djangoproject.com/en/3.0/ref/models/querysets/#update
Upvotes: 2