简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM