Error in Queryset with operator | (union queryset) + slice
class Example:
name = models.CharField(max_length = 30)
public = models.BooleanField()
>>> Example.objects.create('example 1', False)
<Example: Example object>
>>> Example.objects.create('example 2', False)
<Example: Example object>
>>> Example.objects.create('example 3', True)
<Example: Example object>
>>> Example.objects.create('example 4', True)
<Example: Example object>
>>> Example.objects.create('example 5', False)
<Example: Example object>
>>> query = Example.objects.filter(public = True)
>>> if(query.count() < 3):
... query = query | Example.objects.filter(public = False).order_by('?')[:1]
...
>>> query.count()
5
>>>
When using the union of querysets to slice it "ignores" the slice and takes all objects where public = false
.
Change History
(9)
Description: |
modified (diff)
|
Triage Stage: |
Unreviewed → Accepted
|
Owner: |
changed from nobody to Ian Foote
|
Status: |
new → assigned
|
Patch needs improvement: |
set
|
Patch needs improvement: |
unset
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
What should the expected output be? Maybe (pseudoquery):
Or, in this special case this could be written as: