← All challenges
easydjango/django · v3.2

QuerySet.none() on combined queries returns all results.

webormbase 7af8f41273
Mode

60 minutes, the full token budget.

  • Time limit60 min
  • Token budgetup to 1M
  • Worth up to135 XP

Opens VS Code with an AI agent in a new tab. Prompts, tokens, tool calls and test runs are recorded and scored.

Problem statement

Description

I came across this issue on Stack Overflow. I'm not 100% sure it's a bug, but it does seem strange. With this code (excuse the bizarre example filtering):
class Publication(models.Model):
pass
class Article(models.Model):
publications = models.ManyToManyField(to=Publication, blank=True, null=True)
class ArticleForm(forms.ModelForm):
publications = forms.ModelMultipleChoiceField(
Publication.objects.filter(id__lt=2) | Publication.objects.filter(id__gt=5),
required=False,
)
class Meta:
model = Article
fields = ["publications"]
class ArticleAdmin(admin.ModelAdmin):
form = ArticleForm
This works well. However, changing the ModelMultipleChoiceField queryset to use union() breaks things.
publications = forms.ModelMultipleChoiceField(
Publication.objects.filter(id__lt=2).union(
Publication.objects.filter(id__gt=5)
),
required=False,
)
The form correctly shows only the matching objects. However, if you submit this form while empty (i.e. you didn't select any publications), ALL objects matching the queryset will be added. Using the OR query, NO objects are added, as I'd expect.

The environment starts at commit 7af8f4127397 (django/django 3.2), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.