Skip to content

Commit

Permalink
Filter xeditable choices for Foreignkey field with limit_choices_to
Browse files Browse the repository at this point in the history
  • Loading branch information
kvdogan committed May 14, 2021
1 parent bea371b commit 4b95977
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions datatableview/views/xeditable.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ def _get_foreignkey_choices(self, field, field_name):
# will consequently try to assume initial=None, forcing the blank option to appear.
formfield_kwargs['empty_label'] = None
formfield = field.formfield(**formfield_kwargs)

# In case of using Foreignkey limit_choices_to, django prepares model form and handles
# form validation correctly, so does django-datatableview with x-editable plugin. However
# this piece of code helps filtering limited choices to be only visible choices,else
# all the choices are visible.
if formfield.limit_choices_to:
formfield.queryset = formfield.queryset.filter(**formfield.limit_choices_to)
return formfield.choices

def _get_default_choices(self, field, field_name):
Expand Down

0 comments on commit 4b95977

Please sign in to comment.