﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18097	__contains__ on an incompletely evaluated QuerySet can incorrectly return False	Kevin Michel	nobody	"The bug is in [https://code.djangoproject.com/browser/django/trunk/django/db/models/query.py#L135 the function QuerySet.__contains__(self, val)], which is executed when using the python operator `in`.

It can only happen if :
  - `val` is in the `QuerySet`
  - `val` is the last item of the `QuerySet`
  - the `QuerySet` has not been completely evaluated

If all those conditions hold, then :
  - `val` is not yet in `self._result_cache`
  - `self._iter` is not `None`

This makes us jump to the `while True` loop, which runs correctly until we fetch the last item using `self._fill_cache(num=1)`.

When fetching the last item, inside the `self._fill_cache` function, `self._iter` is set to `None`.

Just after fetching the last item, but before comparing its value against `val`, `self.__contains__` checks the value of `self._iter`, which is `None`, and incorrectly return `False`.

The value of the last item should have been checked before checking the value of `self._iter`."	Bug	closed	Database layer (models, ORM)	1.4	Normal	worksforme			Accepted	1	0	1	0	0	0
