django-smart-selects
django-smart-selects copied to clipboard
ChainedModelChoiceField does't honor label_from_instance
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]
- [x] This issue is not about installing previous versions of django-smart-selects older than 1.2.8. I understand that previous versions are insecure and will not receive any support whatsoever.
- [x] I have verified that that issue exists against the
masterbranch of django-smart-selects. - [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [x] I have debugged the issue to the
smart_selectsapp. - [x] I have reduced the issue to the simplest possible case.
- [x] I have included all relevant sections of
models.py,forms.py, andviews.pywith problems. - [x] I have used GitHub Flavored Markdown to style all of my posted code.
Steps to reproduce
- Subclass ChainedModelChoiceField
- 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)
@eduardocesar can you reproduce with latest master version?
@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.
Resolution news?