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 of Max Choices bug #32

Merged
merged 8 commits into from
Nov 1, 2016

Conversation

pjpassa
Copy link
Contributor

@pjpassa pjpassa commented Aug 18, 2016

Max choices used to only send data to the front end if the total number of instances of the model was less than the max choices setting.

I refactored the code so that the set was created earlier. This way the view will return data if the number of unique values is less than the max choices setting.

PJ Passalacqua and others added 6 commits June 7, 2016 13:25
…rwise, the standard text input will be used"

This reverts commit cc19130.
… distinct choices. This allows max_choices to be the maximum unique choices.
This commit changes the way the URLs are declared to avoid RemovedInDjango110Warning messages. The
old way, using django.conf.urls.patterns(), is deprecated and will be removed in Django 1.10
@coveralls
Copy link

coveralls commented Aug 18, 2016

Coverage Status

Coverage decreased (-0.007%) to 96.19% when pulling 2b124aa on worthwhile:feat/max-choices-fix into 119c76d on modlinltd:develop.

@asfaltboy
Copy link
Member

Hi @pjpassa, thanks for the contribution! This fix makes sense but, I'm worried about potentially harmful performance if the query will return a very large number of values.
That was the reason we used query.count() instead, to avoid potentially fetching large amount of values just to count them.

What if we order_by(field.name).distinct().count() instead? For our simple query this should work correctly I believe.

Additionally, I would add a more specific test case, for example leaving the existing name with identical 5 names, and asserting the view returns the 1 distinct name. Later, randomizing the names (or using ID field), and asserting the max limit is reached.

@coveralls
Copy link

coveralls commented Aug 19, 2016

Coverage Status

Coverage decreased (-2.1%) to 94.095% when pulling dbed972 on worthwhile:feat/max-choices-fix into 119c76d on modlinltd:develop.

@coveralls
Copy link

coveralls commented Aug 19, 2016

Coverage Status

Coverage decreased (-0.007%) to 96.19% when pulling dbed972 on worthwhile:feat/max-choices-fix into 119c76d on modlinltd:develop.

@@ -69,8 +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 = set(model_obj.objects.values_list(field.name, flat=True))
if len(choices) <= max_choices:
choices = model_obj.objects.values_list(field.name, flat=True).distinct()
Copy link
Member

@asfaltboy asfaltboy Aug 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope you don't mind me commenting while you work, I think order_by explicitly here is really a must, since the model may have default ordering that differs from our field.name, which may break the distinct query, as noted here.

... if you use a values() query to restrict the columns selected, the columns used in any order_by() (or default model ordering) will still be involved and may affect uniqueness of the results.

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add an explicit order_by in case the model's default queryset manager doesn't order_by a field outside of our values list. This is explained in distinct() docs - first note.

@asfaltboy asfaltboy merged commit 35d7063 into modlinltd:develop Nov 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants