jazzband / django-smart-selects

chained and grouped selects for django forms
https://django-smart-selects.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.11k stars 348 forks source link

"auto_choose=False" is not working #242

Closed fuatu closed 6 years ago

fuatu commented 6 years ago
    tur= models.CharField(max_length=20, verbose_name="Tür",choices=TUR_CHOICES)
    borc = ChainedForeignKey(
        Borc, chained_field = 'tur',  chained_model_field = 'tur', blank=True, null=True,
        show_all=False, auto_choose=False, sort=True, 
        verbose_name="Borc",related_name="borc")

Even auto_choose is false when there is one record it selects the record automatically both in template and in django admin.

czechmate247 commented 6 years ago

I don't know if this is the exact fix for this issue but I found a code modification that seems to be working for me. I changed the following function in the 'chainedfk.js' file:

$.each(j, function (index, optionData) {
       var option = $('<option></option>')
        .prop('value', optionData.value)
        .text(optionData.display);
    if (auto_choose || init_value && optionData.value == init_value) {
        option.prop('selected', true);
    }
    options.push(option);
});

To the function below. I found via browser developer console that "true" or "false" would sometimes be a boolean and sometimes be a string which would change the logic for this function:

$.each(j, function (index, optionData) {
    var option = $('<option></option>')
        .prop('value', optionData.value)
        .text(optionData.display);
    if (auto_choose == "false" || auto_choose == "False"){
        auto_choose = false;
    } else if (auto_choose == "true" || auto_choose == "True"){
        auto_choose = true;
    }
    if (auto_choose || init_value && optionData.value == init_value) {
        option.prop('selected', true);
    }
    options.push(option);
});

I hope this helps the bug fixing efforts.

blag commented 6 years ago

@czechmate247 Thanks for the help! I've added your patch to my PR #250.

@fuatu Please check if my personal master branch fixes this for you and report back. If it does I'll merge it in (as mentioned above, please see PR #250 for progress on that) and cut a release to PyPI so you can install it easier in production. Thanks!

czechmate247 commented 6 years ago

You are most welcome and thank you for implementing this so quickly! I will not be able to check your branch for a few days because I am away from my office. I don’t want to keep you from submitting the changes though so please feel free! Thanks again.

On Jan 21, 2018, at 6:40 PM, blag notifications@github.com wrote:

@czechmate247 https://github.com/czechmate247 Thanks for the help! I've added your patch to my PR #250 https://github.com/digi604/django-smart-selects/pull/250.

@fuatu https://github.com/fuatu Please check if my personal master branch https://github.com/blag/django-smart-selects fixes this for you and report back. If it does I'll merge it in (as mentioned above, please see PR #250 https://github.com/digi604/django-smart-selects/pull/250 for progress on that) and cut a release to PyPI so you can install it easier in production. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/digi604/django-smart-selects/issues/242#issuecomment-359292153, or mute the thread https://github.com/notifications/unsubscribe-auth/AQYLFqtgqa-YaBc8HJVIqC72SuFbgoNMks5tM8r-gaJpZM4Qy6eE.

blag commented 6 years ago

Released to PyPI and GitHub in version 1.5.4. Thanks!