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

ChainedModelChoiceField does't honor label_from_instance #228

Open eduardocesar opened 6 years ago

eduardocesar commented 6 years ago

You MUST use this template when reporting issues. Please make sure you follow the checklist and fill in all of the information sections below.


All versions of django-smart-selects prior to version 1.2.8 are vulnerable to an XSS attack as detailed in issue 171. As a result, all previous versions have been removed from PyPI to prevent users from installing insecure versions. All users are urged to upgrade as soon as possible.

Checklist

Put an x in the bracket when you have completed each task, like this: [x]

Steps to reproduce

  1. Subclass ChainedModelChoiceField
  2. define label_from_instance and return any value

Actual behavior

After defining label_from_instance, the rendered value doesn't show

Expected behavior

The returned value from label_from_instance should be rendered

Relevant Django docs section: https://docs.djangoproject.com/en/1.11/ref/forms/fields/#modelchoicefield

forms.py

class ModifiedModelChoiceField(forms.ModelChoiceField):
    # Works
    def label_from_instance(self, obj):
        return obj.name + ' test'

class ModifiedChainedModelChoiceField(ChainedModelChoiceField):
    # Doesn't work.
    def label_from_instance(self, obj):
        return obj.name + ' test'

class NatalForm(forms.Form):
    nome = forms.CharField(max_length=150)
    continent = forms.ChoiceField(label="Continente", choices=CONTINENT_CHOICES)
    country = ModifiedChainedModelChoiceField(
        queryset=Country.objects.all(),
        empty_label='escolha o país',
        label='País',

        to_app_name='cities_light', to_model_name='Country', chained_field='continent', chained_model_field='continent',
        foreign_key_app_name='openastro', foreign_key_model_name='Location', foreign_key_field_name='country',
        show_all=False, auto_choose=True,
    )

    region = ChainedModelChoiceField(
        #queryset=Region.objects.all(),
        empty_label='escolha o estado',
        label='Estado',

        to_app_name='cities_light', to_model_name='Region', chained_field='country', chained_model_field='country',
        foreign_key_app_name='openastro', foreign_key_model_name='Location', foreign_key_field_name='region',
        show_all=False, auto_choose=True,
    )

    city = ChainedModelChoiceField(
        #queryset=City.objects.all(),
        empty_label='escolha a cidade',
        label='Cidade',

        to_app_name='cities_light', to_model_name='City', chained_field='region', chained_model_field='region',
        foreign_key_app_name='openastro', foreign_key_model_name='Location', foreign_key_field_name='city',
        show_all=False, auto_choose=True,
    )

models.py

@python_2_unicode_compatible
class Location(models.Model):
    continent = models.CharField(max_length=2, choices=CONTINENT_CHOICES)
    country = ChainedForeignKey(
        Country,
        chained_field="continent",
        chained_model_field="continent",
        show_all=False,
        auto_choose=True,
        sort=True)

    region = ChainedForeignKey(
        Region,
        chained_field="country",
        chained_model_field="country",
        show_all=False,
        auto_choose=True,
        sort=True)

    city = ChainedForeignKey(
        City,
        chained_field="region",
        chained_model_field="region",
        show_all=False,
        auto_choose=True,
        sort=True)

    def __str__(self):
        return str(self.city)
manelclos commented 4 years ago

@eduardocesar can you reproduce with latest master version?

manelclos commented 3 years ago

@eduardocesar I can confirm this problem, but when trying to fix it I found it is not a one-liner, so this would have to wait until a major release.

Gigiarum commented 3 years ago

Resolution news?