Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix verbose field name util for lazy text #558

Merged
merged 2 commits into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions django_filters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ def verbose_field_name(model, field_name):
names = []
for part in parts:
if isinstance(part, ForeignObjectRel):
names.append(part.related_name)
names.append(force_text(part.related_name))
else:
names.append(part.verbose_name)
names.append(force_text(part.verbose_name))

return ' '.join(names)

Expand Down
3 changes: 2 additions & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django import forms
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _


REGULAR = 0
Expand Down Expand Up @@ -45,7 +46,7 @@ def formfield(self, **kwargs):

@python_2_unicode_compatible
class User(models.Model):
username = models.CharField(max_length=255)
username = models.CharField(_('username'), max_length=255)
first_name = SubCharField(max_length=100)
last_name = SubSubCharField(max_length=100)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db import models
from django.db.models.constants import LOOKUP_SEP
from django.db.models.fields.related import ForeignObjectRel
from django.utils.functional import Promise

from django_filters.utils import (
get_field_parts, get_model_field, resolve_field,
Expand Down Expand Up @@ -232,6 +233,14 @@ def test_backwards_related_field(self):
verbose_name = verbose_field_name(Book, 'lovers__first_name')
self.assertEqual(verbose_name, 'lovers first name')

def test_lazy_text(self):
# sanity check
field = User._meta.get_field('username')
self.assertIsInstance(field.verbose_name, Promise)

verbose_name = verbose_field_name(User, 'username')
self.assertEqual(verbose_name, 'username')


class VerboseLookupExprTests(TestCase):

Expand Down