Attilio
Attilio

Reputation: 77

Django queryset return different value for the same element

I'm baffled... here is what I get if I'm querying a very simple model:

python manage.py shell
In [1]: from tm_repository.models import TMTable
In [2]: a=TMTable.objects.filter(sourceVC__contains='this activates the function for stating the access level of the input')
In [3]: a.count()
Out[3]: 8
In [4]: a[7].sourceVC
Out[4]: u'Select {1}{2}2{3}{4}; this activates the function for stating the access level of the input line in question, {5}Change Line Level [0-7]{6} appears on the display.'
In [5]: a[7].sourceVC
Out[5]: u'this activates the function for stating the access level of the input line in question, Change Line Level [0-7] appears on the display.'

the object changed! I mean, what's going on? As far as I know a list in python is persistent and reading the django documentations I didn't find anything that can justify this strange behavior...

What am I doing wrong?

Upvotes: 1

Views: 608

Answers (1)

akaariai
akaariai

Reputation: 734

SQL queries aren't ordered by default. The database has returned you a different instance from different ordering of the results. Add order_by to the query and you will get the same instance back (as long as there aren't concurrent edits).

Upvotes: 1

Related Questions