﻿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
35587	Add QuerySet.partition(*args, **kwargs)	Micah Cantor		"A common task with a Django model is to partition the model instances into two sets, ones that is selected by some filters, and ones that are not. Naively, the following utility script can accomplish this with QuerySet.filter() and QuerySet.exclude()

{{{
from django.db.models import QuerySet
from django.db.models.manager import BaseManager

def partition(self, *args, **kwargs):
    filtered = self.filter(*args, **kwargs)
    excluded = self.exclude(*args, **kwargs)
    return filtered, excluded

QuerySet.partition = partition
BaseManager.partition = partition
}}}

For instance, if we have a Book model, we can divide it into those that are fiction and nonfiction.

{{{
fiction, nonfiction = Book.objects.partition(genre=""fiction"")
}}}

Obtaining two separate QuerySets is often helpful if we want add further filters, ordering, or prefetches to one set but not the other. 

Adding this method to Django would be a helpful utility, and could also be implemented more efficiently than my own naive implementation. It would be difficult for me to suggest a better implementation without a deeper understanding of the implementations of filter() and exclude()."	New feature	closed	Database layer (models, ORM)	5.0	Normal	wontfix			Unreviewed	0	0	0	0	0	0
