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

Assumes the values and choices are in the same order #23

Closed
tiagob opened this issue Jan 18, 2013 · 1 comment
Closed

Assumes the values and choices are in the same order #23

tiagob opened this issue Jan 18, 2013 · 1 comment
Labels

Comments

@tiagob
Copy link

tiagob commented Jan 18, 2013

I noticed the issue when I tried to delete a selected option. The option I thought I deleted still appeared after I successfully submitted the form. This seems to only happen when I have a lot of selected options (I have ~180 options selected and ~5k choices loaded via ajax).

The issue occurred because data in heavy_data.js wasn't correct. The ids didn't correctly match the txts.

I think this is because in heavy_data.js it's assumed that you can zip up txts and vals and this will yield the correct data objects (lines 46-48):

$(vals).each(function (index) {
    data.push({id: this, text: txts[index]});
});

But when these values are created in widgets.py vals and texts are not created with the same order. Vals is in it's instantiated order but texts is in the order that the matching val appears in choices.

In the render_texts method I think these lines:

for val, txt in chain(self.choices, all_choices):
    val = force_unicode(val)
    if val in selected_choices:
        selected_choices = [v for v in selected_choices if v != val]
        txts.append(txt)

should be changed to something like:

choices_dict = dict(chain(self.choices, all_choices))
for val in selected_choices:
    try:
        txts.append(choices_dict[val])
    except KeyError:
        raise KeyError("Value %s not in choices" % val)
@applegrew
Copy link
Owner

Fix merged in Master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants