Ticket #14766: 14766_fix_with_tests.diff
File 14766_fix_with_tests.diff, 2.1 KB (added by , 14 years ago) |
---|
-
django/db/models/sql/query.py
1554 1554 raise FieldError('Invalid order_by arguments: %s' % errors) 1555 1555 if ordering: 1556 1556 self.order_by.extend(ordering) 1557 # Validate field names 1558 for field in ordering: 1559 if field != '?' and not isinstance(field, int) and '.' not in field: 1560 col, order = get_order_dir(field) 1561 if col not in (self.aggregate_select.keys() \ 1562 + self.extra_select.keys()): 1563 # This is called to trigger any applicable FieldErrors 1564 self.setup_joins(col.split(LOOKUP_SEP), 1565 self.model._meta, 1566 self.get_initial_alias(), 1567 False) 1557 1568 else: 1558 1569 self.default_ordering = False 1559 1570 -
tests/regressiontests/model_fields/tests.py
3 3 4 4 from django import test 5 5 from django import forms 6 from django.core.exceptions import ValidationError6 from django.core.exceptions import FieldError, ValidationError 7 7 from django.db import models 8 8 from django.db.models.fields.files import FieldFile 9 9 from django.utils import unittest … … 348 348 field = d._meta.get_field('myfile') 349 349 field.save_form_data(d, 'else.txt') 350 350 self.assertEqual(d.myfile, 'else.txt') 351 352 class FieldErrorTest(unittest.TestCase): 353 def test_field_error(self): 354 """ 355 Test that order_by arguments are validated without executing the query. 356 """ 357 # Regression test for #14766 358 self.assertRaises(FieldError, Foo.objects.order_by, 'nonexistent_field') 359 self.assertRaises(FieldError, Bar.objects.order_by, 'a__nonexistent_field')