@@ -424,6 +424,10 @@ class OrderingFilterModel(models.Model):
424
424
title = models .CharField (max_length = 20 , verbose_name = 'verbose title' )
425
425
text = models .CharField (max_length = 100 )
426
426
427
+ @property
428
+ def description (self ):
429
+ return self .title + ": " + self .text
430
+
427
431
428
432
class OrderingFilterRelatedModel (models .Model ):
429
433
related_object = models .ForeignKey (OrderingFilterModel , related_name = "relateds" , on_delete = models .CASCADE )
@@ -436,6 +440,17 @@ class Meta:
436
440
fields = '__all__'
437
441
438
442
443
+ class OrderingFilterSerializerWithModelProperty (serializers .ModelSerializer ):
444
+ class Meta :
445
+ model = OrderingFilterModel
446
+ fields = (
447
+ "id" ,
448
+ "title" ,
449
+ "text" ,
450
+ "description"
451
+ )
452
+
453
+
439
454
class OrderingDottedRelatedSerializer (serializers .ModelSerializer ):
440
455
related_text = serializers .CharField (source = 'related_object.text' )
441
456
related_title = serializers .CharField (source = 'related_object.title' )
@@ -551,6 +566,42 @@ class OrderingListView(generics.ListAPIView):
551
566
{'id' : 1 , 'title' : 'zyx' , 'text' : 'abc' },
552
567
]
553
568
569
+ def test_ordering_without_ordering_fields (self ):
570
+ class OrderingListView (generics .ListAPIView ):
571
+ queryset = OrderingFilterModel .objects .all ()
572
+ serializer_class = OrderingFilterSerializerWithModelProperty
573
+ filter_backends = (filters .OrderingFilter ,)
574
+ ordering = ('title' ,)
575
+
576
+ view = OrderingListView .as_view ()
577
+
578
+ # Model field ordering works fine.
579
+ request = factory .get ('/' , {'ordering' : 'text' })
580
+ response = view (request )
581
+ assert response .data == [
582
+ {'id' : 1 , 'title' : 'zyx' , 'text' : 'abc' , 'description' : 'zyx: abc' },
583
+ {'id' : 2 , 'title' : 'yxw' , 'text' : 'bcd' , 'description' : 'yxw: bcd' },
584
+ {'id' : 3 , 'title' : 'xwv' , 'text' : 'cde' , 'description' : 'xwv: cde' },
585
+ ]
586
+
587
+ # `incorrectfield` ordering works fine.
588
+ request = factory .get ('/' , {'ordering' : 'foobar' })
589
+ response = view (request )
590
+ assert response .data == [
591
+ {'id' : 3 , 'title' : 'xwv' , 'text' : 'cde' , 'description' : 'xwv: cde' },
592
+ {'id' : 2 , 'title' : 'yxw' , 'text' : 'bcd' , 'description' : 'yxw: bcd' },
593
+ {'id' : 1 , 'title' : 'zyx' , 'text' : 'abc' , 'description' : 'zyx: abc' },
594
+ ]
595
+
596
+ # `description` is a Model property, which should be ignored.
597
+ request = factory .get ('/' , {'ordering' : 'description' })
598
+ response = view (request )
599
+ assert response .data == [
600
+ {'id' : 3 , 'title' : 'xwv' , 'text' : 'cde' , 'description' : 'xwv: cde' },
601
+ {'id' : 2 , 'title' : 'yxw' , 'text' : 'bcd' , 'description' : 'yxw: bcd' },
602
+ {'id' : 1 , 'title' : 'zyx' , 'text' : 'abc' , 'description' : 'zyx: abc' },
603
+ ]
604
+
554
605
def test_default_ordering (self ):
555
606
class OrderingListView (generics .ListAPIView ):
556
607
queryset = OrderingFilterModel .objects .all ()
0 commit comments