[Solved]-Changing model field within the Django Shell


You should save the changes,

game = Game.objects.get(name="testb")
game.likes = 5


Calling Game.objects.get() retrieves the data from the database.

When you execute the statement Game.objects.get(name='test').likes = 5, you are retrieving the data from the database, creating a python object, and then setting a field on that object in memory.

Then, when you run Game.objects.get(name='test') again, you are re-pulling the data from the database and loading a python object into memory. Note that above, when you set likes to 5, you did that purely in memory and never saved the data to the database. This is why when you re-pull the data, likes is 0.

If you want the data to be persisted, you have to call game.save() after setting the likes field. This will enter the data into the database, so that the next time you retrieve it via .get(), your changes will have persisted.



If u need change all fields for all items, just try this:

from shop.models import Product


U can use this in Django Shell. Have a nice time 🙂

Leave a comment