thechan = Score.objects.filter(content=44)[0:1]
thechan[0].custom_score = 2
thechan[0].save()
I do print statements, and it shows everything fine. However, it’s not SAVING!
I go into my database, and I run a simple SELECT statement..and it’s not changed!
select custom_score FROM music_score where content_id = 44;
What’s going on here is that Score.objects.filter() doesn’t return a regular list, but a QuerySet. QuerySets behave like lists in some ways, but every time you slice one you get a new QuerySet instance, and everytime you index into one, you get a new instance of your model class.
That means your original code does something like:
If for whatever reason you needed to do this on a QuerySet rather than just using get(), you could write:
instead. This distinction becomes a bit more important if you are, say, iterating over the elements of a QuerySet instead of dealing with a single record.