From 4b959772bd305aa0fd747bcff7cdf88b388dd214 Mon Sep 17 00:00:00 2001 From: kvdogan Date: Fri, 14 May 2021 13:55:15 +0200 Subject: [PATCH] Filter xeditable choices for Foreignkey field with limit_choices_to --- datatableview/views/xeditable.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/datatableview/views/xeditable.py b/datatableview/views/xeditable.py index 89614cf..88af3af 100644 --- a/datatableview/views/xeditable.py +++ b/datatableview/views/xeditable.py @@ -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):