mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 11:04:02 +03:00
OrderingFilter adjustements (#3983)
* Made sure the OrderingFilter relies on Field.verbose_name. * Marked OrderingFilter's order labels for translation.
This commit is contained in:
parent
95418eb8ac
commit
d87f2bc7b6
|
@ -240,8 +240,7 @@ class OrderingFilter(BaseFilterBackend):
|
||||||
elif valid_fields == '__all__':
|
elif valid_fields == '__all__':
|
||||||
# View explicitly allows filtering on any model field
|
# View explicitly allows filtering on any model field
|
||||||
valid_fields = [
|
valid_fields = [
|
||||||
(field.name, getattr(field, 'label', field.name.title()))
|
(field.name, field.verbose_name) for field in queryset.model._meta.fields
|
||||||
for field in queryset.model._meta.fields
|
|
||||||
]
|
]
|
||||||
valid_fields += [
|
valid_fields += [
|
||||||
(key, key.title().split('__'))
|
(key, key.title().split('__'))
|
||||||
|
@ -272,8 +271,8 @@ class OrderingFilter(BaseFilterBackend):
|
||||||
current = None if current is None else current[0]
|
current = None if current is None else current[0]
|
||||||
options = []
|
options = []
|
||||||
for key, label in self.get_valid_fields(queryset, view):
|
for key, label in self.get_valid_fields(queryset, view):
|
||||||
options.append((key, '%s - ascending' % label))
|
options.append((key, '%s - %s' % (label, _('ascending'))))
|
||||||
options.append(('-' + key, '%s - descending' % label))
|
options.append(('-' + key, '%s - %s' % (label, _('descending'))))
|
||||||
return {
|
return {
|
||||||
'request': request,
|
'request': request,
|
||||||
'current': current,
|
'current': current,
|
||||||
|
|
|
@ -499,7 +499,7 @@ class SearchFilterM2MTests(TestCase):
|
||||||
|
|
||||||
|
|
||||||
class OrderingFilterModel(models.Model):
|
class OrderingFilterModel(models.Model):
|
||||||
title = models.CharField(max_length=20)
|
title = models.CharField(max_length=20, verbose_name='verbose title')
|
||||||
text = models.CharField(max_length=100)
|
text = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
|
@ -741,6 +741,19 @@ class OrderingFilterTests(TestCase):
|
||||||
|
|
||||||
reload_module(filters)
|
reload_module(filters)
|
||||||
|
|
||||||
|
def test_get_template_context(self):
|
||||||
|
class OrderingListView(generics.ListAPIView):
|
||||||
|
ordering_fields = '__all__'
|
||||||
|
serializer_class = OrderingFilterSerializer
|
||||||
|
queryset = OrderingFilterModel.objects.all()
|
||||||
|
filter_backends = (filters.OrderingFilter,)
|
||||||
|
|
||||||
|
request = factory.get('/', {'ordering': 'title'}, HTTP_ACCEPT='text/html')
|
||||||
|
view = OrderingListView.as_view()
|
||||||
|
response = view(request)
|
||||||
|
|
||||||
|
self.assertContains(response, 'verbose title')
|
||||||
|
|
||||||
|
|
||||||
class SensitiveOrderingFilterModel(models.Model):
|
class SensitiveOrderingFilterModel(models.Model):
|
||||||
username = models.CharField(max_length=20)
|
username = models.CharField(max_length=20)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user