A column may only be included once (the first occurrence) so we check to ensure there are no duplicates by inspecting the SQL.
(self)
| 507 | ) |
| 508 | |
| 509 | def test_order_by_f_expression_duplicates(self): |
| 510 | """ |
| 511 | A column may only be included once (the first occurrence) so we check |
| 512 | to ensure there are no duplicates by inspecting the SQL. |
| 513 | """ |
| 514 | qs = Article.objects.order_by(F("headline").asc(), F("headline").desc()) |
| 515 | sql = str(qs.query).upper() |
| 516 | fragment = sql[sql.find("ORDER BY") :] |
| 517 | self.assertEqual(fragment.count("HEADLINE"), 1) |
| 518 | self.assertQuerySetEqual( |
| 519 | qs, |
| 520 | [ |
| 521 | "Article 1", |
| 522 | "Article 2", |
| 523 | "Article 3", |
| 524 | "Article 4", |
| 525 | ], |
| 526 | attrgetter("headline"), |
| 527 | ) |
| 528 | qs = Article.objects.order_by(F("headline").desc(), F("headline").asc()) |
| 529 | sql = str(qs.query).upper() |
| 530 | fragment = sql[sql.find("ORDER BY") :] |
| 531 | self.assertEqual(fragment.count("HEADLINE"), 1) |
| 532 | self.assertQuerySetEqual( |
| 533 | qs, |
| 534 | [ |
| 535 | "Article 4", |
| 536 | "Article 3", |
| 537 | "Article 2", |
| 538 | "Article 1", |
| 539 | ], |
| 540 | attrgetter("headline"), |
| 541 | ) |
| 542 | |
| 543 | def test_order_by_constant_value(self): |
| 544 | # Order by annotated constant from selected columns. |