django-mongodb-engine/mongodb-engine — Issue #85: AssertionError on identical chained queries
Author: emperorcezar
Created: 2011-11-11 18:51:18+00:00
State: open

The complier throws an error when there are two chained identical filters. The case that brought me to find this was when I creating a form for the admin.

I have the following line of code:

```python
self.fields['faculty'].queryset = User.objects.filter(pk__in = [faculty.user.pk for faculty in faculty_list])
```

In the case where there is only one faculty user, and I create the object (in the admin form) selecting that one user, I will get an error triggered by the following line of `django_mongodb_engine/compiler.py` on line 245:

```python
assert all(key not in existing for key in lookup.keys()), [lookup, existing]
```

What is causing this case to occur is the following code in the clean() method of Django's `ModelChoiceField`

```python
qs = self.queryset.filter(**{'%s__in' % key: value})
```

where the value is the choice I selected.

Puting both of these together you get the query

```python
User.objects.filter(pk__in = [u'4eaef5d7bb6933592e000010']).filter(pk__in = [u'4eaef5d7bb6933592e000010'])
```

and then the `AssertionError` triggers.

I'm trying to work on a patch right now, but I'm not real familiar with the complier. Any advice appreciated.