usage of iterator() on django queryset

后端 未结 2 1778
难免孤独
难免孤独 2021-02-12 14:57

I came across some strange behaviour recently, and need to check my understanding.

I\'m using a simple filter in the model and then iterating over the results.

e

2条回答
  •  醉酒成梦
    2021-02-12 15:19

    oddly, it was returning only a partial list of books.

    That's not how the queryset must work. Iterating over queryset should give you every record returned by your database. Debug your code. You'll find the error, otherwise debug it again.

    It's easy to check in the REPL. Run manage.py shell:

    from app.models import Model
    for o in Model.objects.filter(fieldname="foo"): print o
    
    #Let's see DB query
    from django.db import connection
    print(connection.queries)
    

提交回复
热议问题