django-smart-selects
django-smart-selects copied to clipboard
Empty dropdowns when ModelForm is Edited
The dependant dropdowns are not populated when the queryset of the ModelForm is changed - or the ModelForm is used for Editing(i.e, an instance is loaded for editing).
ie., if person is the chained_field in my form and I changed its queryset before rendering as follows, then the dependent dropdowns do not get populated while the form is rendered.
form.fields['person'].queryset = this_dept.get_persons()
Edit (by blag): Formatted code with GFM.
Does this still happen if you use the master branch? Can you post any relevant excerpts from the problematic models.py
, your Python/Django/django-smart-selects versions, any errors that appear in your browser console, and any other information that would help me troubleshoot this? Thanks!
It looks like this is now supported for all chained relation form fields:
if 'queryset' not in kwargs:
queryset = get_model(to_app_name, to_model_name).objects.all()
super(ChainedModelChoiceField, self).__init__(queryset=queryset, initial=initial, *args, **defaults)
else:
super(ChainedModelChoiceField, self).__init__(initial=initial, *args, **defaults)
if 'queryset' not in kwargs:
queryset = get_model(to_app_name, to_model_name).objects.all()
super(ChainedManyToManyField, self).__init__(queryset=queryset, initial=initial, *args, **defaults)
else:
super(ChainedManyToManyField, self).__init__(initial=initial, *args, **defaults)
def __init__(self, queryset, order_field, *args, **kwargs):
self.order_field = order_field
super(GroupedModelSelect, self).__init__(queryset, *args, **kwargs)
Due to that, I'm closing this. Feel free to reopen if the problem still occurs.
@blag i'm having the same issue right now, i have the most recent version of the repo and i have the lines that you mention on the previous comment, still not working, please re open the issue.
Thanks
I think this is a serious problem, i'm trying to debug it even at Django's level but no succeed finding the issue.
@MiltonLn Can you post the information I requested in comment 255518778?
Ok,
smart-selects version: 1.2.6 django: 1.10.2 Python: 3.5.2
No python console errors, No Javascript console errors,
At database level it works, i think probably the issue is at django's form instance level or widget level
@MiltonLn Sorry, I missed your last comment. Did you ever get to the root of the issue?
I have this issue with ChainedManyToManyField. Not setting the default selected items when edit :/
@MiltonLn @sebitoelcheater Can you give me more information to troubleshoot? Specifically:
- HTML snippets before/after editing the form
- Any errors in the browser console
- Is the AJAX request firing?
- What does the AJAX response (if there is one) look like?
Thanks!
looks like an issue similar to this one https://github.com/digi604/django-smart-selects/issues/253#issuecomment-379346137
the code at line 50 in chainedfk.js assumes that the rest of the code iterates over the response object, but that's not true.
if you add a new line at line 50
j = j.response;
it should basically work
I have submitted a PR https://github.com/digi604/django-smart-selects/pull/259
Anyone having this issue, please test with latest release, thanks!