Ticket #14766: 14766_fix_with_tests.diff

File 14766_fix_with_tests.diff, 2.1 KB (added by Aram Dulyan, 9 years ago)

Fix with tests (derived from patch above)

  • django/db/models/sql/query.py

     
    15541554            raise FieldError('Invalid order_by arguments: %s' % errors)
    15551555        if ordering:
    15561556            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)
    15571568        else:
    15581569            self.default_ordering = False
    15591570
  • tests/regressiontests/model_fields/tests.py

     
    33
    44from django import test
    55from django import forms
    6 from django.core.exceptions import ValidationError
     6from django.core.exceptions import FieldError, ValidationError
    77from django.db import models
    88from django.db.models.fields.files import FieldFile
    99from django.utils import unittest
     
    348348        field = d._meta.get_field('myfile')
    349349        field.save_form_data(d, 'else.txt')
    350350        self.assertEqual(d.myfile, 'else.txt')
     351
     352class 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')
Back to Top