I have a question regarding the filter() and all() methods of django objects. It is not a question which one of the two is preferred, I just noticed an odd (to me) behavior. Because, as it is laid out in
Django ORM - objects.filter() vs. objects.all().filter() - which one is preferred?
in Django src, both ways should return the same (they both reference the chain() method):
See:
And:
So the filter() and all() method should return the same objects. But I recently discovered the following behavior:
MyModel.objects.all()[0].update(name="Test")
# --> $: AttributeError: type object 'MyModel' has no attribute 'update'
# And to check if it indeed has no update method:
MyModel.objects.all[0].__dir__() # --> no update() method in returned dictionary but a save method
So while above code raises Error, line below would work:
MyModel.objects.all()[0].name = "Test"
MyModel.objects.all()[0].save()
However, if the same object is retrieved by the filter() method, it has the update() method.
Why do I get the same object both times but with seemingly different methods added to it?