django-smart-selects icon indicating copy to clipboard operation
django-smart-selects copied to clipboard

Empty dropdowns when ModelForm is Edited

Open venkasub opened this issue 10 years ago • 12 comments

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.

venkasub avatar May 06 '14 17:05 venkasub

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!

blag avatar Oct 22 '16 09:10 blag

It looks like this is now supported for all chained relation form fields:

form_fields.py:25:

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)

form_fields.py:50:

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)

form_fields.py:58:

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 avatar Oct 31 '16 20:10 blag

@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

MiltonLn avatar Oct 31 '16 21:10 MiltonLn

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 avatar Oct 31 '16 21:10 MiltonLn

@MiltonLn Can you post the information I requested in comment 255518778?

blag avatar Oct 31 '16 21:10 blag

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 avatar Oct 31 '16 21:10 MiltonLn

@MiltonLn Sorry, I missed your last comment. Did you ever get to the root of the issue?

blag avatar Jan 01 '17 22:01 blag

I have this issue with ChainedManyToManyField. Not setting the default selected items when edit :/

sebitoelcheater avatar Jan 02 '17 05:01 sebitoelcheater

@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!

blag avatar May 10 '17 23:05 blag

looks like an issue similar to this one https://github.com/digi604/django-smart-selects/issues/253#issuecomment-379346137

openrijal avatar Apr 06 '18 19:04 openrijal

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

openrijal avatar Apr 06 '18 19:04 openrijal

Anyone having this issue, please test with latest release, thanks!

manelclos avatar Aug 18 '20 05:08 manelclos