1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Do you mean that each time I call a[7] a new instance is created? That's really strange... that means I cannot use a simple loop to read the queryset results... are you 100% that this is true?
Yes, this is what is happening. You can turn the queryset into a list by doing a = list(a). After that a is just a regular Python list.
well, then I have to review all my code because I'm not sure I didn't use somewhere a simple loop on a queryset accessing all the elements with that sintax... thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.