Skip to content

Commit

Permalink
Merge pull request #32 from worthwhile/feat/max-choices-fix
Browse files Browse the repository at this point in the history
Fix of Max Choices bug
  • Loading branch information
asfaltboy committed Nov 1, 2016
2 parents 119c76d + dbed972 commit 35d7063
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 9 additions & 1 deletion advanced_filters/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ def test_database_choices(self):
def test_more_than_max_database_choices(self):
factories.Client.create_batch(5, assigned_to=self.user)
view_url = reverse(self.url_name, kwargs=dict(
model='customers.Client', field_name='first_name'))
model='customers.Client', field_name='id'))
res = self.client.get(view_url)
self.assert_json(res, {'results': []})

@override_settings(ADVANCED_FILTERS_MAX_CHOICES=4)
def test_distinct_database_choices(self):
factories.Client.create_batch(5, assigned_to=self.user, email="foo@bar.com")
view_url = reverse(self.url_name, kwargs=dict(
model='customers.Client', field_name='email'))
res = self.client.get(view_url)
self.assert_json(res, {'results': [{'id': 'foo@bar.com', 'text': 'foo@bar.com'}]})
5 changes: 2 additions & 3 deletions advanced_filters/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ def get(self, request, model=None, field_name=None):
logger.debug('No choices calculated for field %s of type %s',
field, type(field))
else:
choices = model_obj.objects.values_list(field.name, flat=True)
if choices.count() < max_choices:
choices = set(choices)
choices = model_obj.objects.values_list(field.name, flat=True).distinct()
if choices.count() <= max_choices:
choices = zip(choices, choices)
logger.debug('Choices found for field %s: %s',
field.name, choices)
Expand Down

0 comments on commit 35d7063

Please sign in to comment.