Ordering¶
OrderingFilterBackend
¶
Bases: OrderingFilter
Custom OrderingFilter with additional features.
Provides: - Better support of openapi via drf-spectacular. - Extra kwargs for ordering fields.
Source code in saritasa_drf_tools/filters/ordering.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
get_ordering(request, queryset, view)
¶
Adjust ordering params.
Wrap ordering fields in models.OrderBy and pass extra ordering kwargs
When using PostgreSQL and sorting by non-unique fields only (or without
any ordering), non-deterministic results may occur, because the db
doesn't guarantee particular output ordering without unique ordering
fields
(https://www.postgresql.org/docs/current/queries-order.html).
Closed PR to change this in django-rest-framework:
https://github.com/encode/django-rest-framework/pull/9109
To fix this, "id" is added as the last ordering field if
add_pk_to_ordering is provided (otherwise applied by default or
controlled via global setting). If "id" is already present in
ordering, it won't be duplicated.
Source code in saritasa_drf_tools/filters/ordering.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
get_schema_operation_parameters(view)
¶
Prepare parameters for openapi schema.
Check that view has ordering_fields.
Check that ordering_fields contains valid set of fields. Actually,
this check may perform some SQL queries during spec generation. Also,
spec generation is not the best place for checking of source code
(comparing to linters/django system checks/tests), but DRF doesn't
validate ordering_fields for views while backend running.
Extend view description with list of ordering_fields.
Source code in saritasa_drf_tools/filters/ordering.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |