          # Bad field requests in aggregates are caught and reported
          with self.assertRaises(FieldError):
              Book.objects.all().aggregate(num_authors=Count('foo'))
  
          with self.assertRaises(FieldError):
              Book.objects.all().annotate(num_authors=Count('foo'))
  
          with self.assertRaises(FieldError):
              Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('foo'))
  
 
</s> remove         with self.assertRaises(FieldError):
 </s> add         msg = (
            "Cannot resolve keyword 'foo' into field. Choices are: authors, "
            "contact, contact_id, hardbackbook, id, isbn, name, pages, price, "
            "pubdate, publisher, publisher_id, rating, store, tags"
        )
        with self.assertRaisesMessage(FieldError, msg): </s> remove         with self.assertRaises(FieldError):
 </s> add         msg = (
            "Cannot resolve keyword 'foo' into field. Choices are: authors, "
            "contact, contact_id, hardbackbook, id, isbn, name, num_authors, "
            "pages, price, pubdate, publisher, publisher_id, rating, store, tags"
        )
        with self.assertRaisesMessage(FieldError, msg): </s> remove             with self.assertRaises(FieldError):
 </s> add             msg = "Joined field references are not permitted in this query"
            with self.assertRaisesMessage(FieldError, msg): </s> remove         with self.assertRaises(FieldError):
 </s> add         msg = "Unsupported lookup 'blahblah' for CharField or join on the field not permitted."
        with self.assertRaisesMessage(FieldError, msg): </s> remove         with self.assertRaises(FieldError):
 </s> add         msg = 'Joined field references are not permitted in this query'
        with self.assertRaisesMessage(FieldError, msg):
        with self.assertRaisesMessage(FieldError, msg):
          # Bad field requests in aggregates are caught and reported
          with self.assertRaises(FieldError):
              Book.objects.all().aggregate(num_authors=Count('foo'))
  
        with self.assertRaisesMessage(FieldError, msg):
              Book.objects.all().annotate(num_authors=Count('foo'))
  
          with self.assertRaises(FieldError):
              Book.objects.all().annotate(num_authors=Count('authors__id')).aggregate(Max('foo'))
  
keep keep keep keep replace keep keep keep keep keep
 Used assertRaisesMessage() to test Django's error messages. 
tests/aggregation_regress/tests.py