Skip to content

Instantly share code, notes, and snippets.

@manfre
Created October 8, 2013 12:42
Show Gist options
  • Save manfre/6884088 to your computer and use it in GitHub Desktop.
Save manfre/6884088 to your computer and use it in GitHub Desktop.
class ItemGroup(models.Model):
name = models.CharField(max_length=10)
group_type = models.CharField(max_length=10, default='test')
class Item(models.Model):
group = models.ForeignKey(ItemGroup,
related_name='items',
limit_choices_to={
'group_type__in': ('test'),
},
)
name = models.CharField(max_length=10)
class Meta:
ordering = ('group__group_type', 'name')
from django.db.models import Q
from django.test import TestCase
from .models import ItemGroup, Item
class SlicingRegressionTests(TestCase):
def test_order_from_foreign_key(self):
"""
Page a query that is ordered by a column from the foreign key.
"""
group1 = ItemGroup.objects.create(name='group1')
group1.items.create(name='g1 item1')
group1.items.create(name='g1 item2')
group1.items.create(name='g1 item3')
group2 = ItemGroup.objects.create(name='group2')
group2.items.create(name='g2 item1')
group2.items.create(name='g2 item2')
group3 = ItemGroup.objects.create(name='group3')
group3.items.create(name='g3 item1')
group3.items.create(name='g3 item1')
qs = Item.objects.filter(
Q(name__icontains='item')
)[5:7]
self.assertEqual(2, len(qs))
for item in qs:
self.assertTrue(item.name.startswith('g3'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment