def test_prefetch_related(self):
    apple = self.food_model.objects.create(name='apple')
    apple.tags.add('1', '2')
    orange = self.food_model.objects.create(name='orange')
    orange.tags.add('2', '4')
    with self.assertNumQueries(2):
        l = list(self.food_model.objects.prefetch_related('tags').all())
    with self.assertNumQueries(0):
        foods = dict(((f.name, set((t.name for t in f.tags.all()))) for f in l))
        self.assertEqual(foods, {'orange': set(['2', '4']), 'apple': set(['1', '2'])})
def test_prefetch_related(self):
    apple = self.food_model.objects.create(name='apple')
    apple.tags.add('1', '2')
    orange = self.food_model.objects.create(name='orange')
    orange.tags.add('2', '4')
    with self.assertNumQueries(2):
        l = list(self.food_model.objects.prefetch_related('tags').all())
    with self.assertNumQueries(0):
        foods = dict(((f.name, set((t.name for t in f.tags.all()))) for f in l))
        self.assertEqual(foods, {'orange': set(['2', '4']), 'apple': set(['1', '2'])})