0

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
4
  • Do you know the primary key? Exactly what object do you want to update? Commented Jul 13, 2020 at 21:30
  • @WillemVanOnsem yes they are pk 1 and 2 as at the moment they are the only 2 entries in the table. However these entries will grow in the future probably if i need to add more things to the table. I have updated the question to show the error that occurs in the terminal. Commented Jul 13, 2020 at 23:30
  • This is not actual code, cause managers don't work on instances. So no clue what objects is here. This could be a case of a model with a Manager that is not an instance but a class? I.e.: class PF(models.Model): objects = models.Manager # no () Commented Jul 13, 2020 at 23:39
  • I am running from a file called ‘main.py’, that is held in my app called ‘charts app’. In order to process the ‘main.py’ file I enter my project manage shell: ‘python3 manage.py shell’ and then I do ‘from chartsapp import main’ and the ‘main.py’ file runs automatically. Commented Jul 13, 2020 at 23:46

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.