Making Django-Jet autocomplete with TabularInlines

1.4k Views Asked by At

My team has been using "django-jet" as a skin to the admin in our django application.

We have inlines with very large select widgets, so as the django-jet documentation recommends, we added autocomplete fields to our models.

The problem is that it doesn't support TabularInlines. Inspecting django-jet's code you can see how what to change to make the widget work:

  • Copy Django's admin "tabular.html" in your template dirs.
  • Load jet_tags in the first line.
  • Add the filter jet_select2_lookup to the fields.

tabular.html

1 {% load i18n admin_urls static admin_modify jet_tags %}
...
...
55               {% if field.is_readonly %}
56                   <p>{{ field.contents }}</p>
57               {% else %}
58                   {{ field.field.errors.as_ul }}
59                   {{ field.field|jet_select2_lookups }}
60               {% endif %}

This approach works to add the autocomplete widget, but it fails with a "invalid option" error.

At the same time, the widget doesn't seems to use the formfield_for_foreignkey method defined in the admin, so the queryset isn't filtered.

Has anyone successfully added autocomplete to a TabularInline using django-jet?

2

There are 2 best solutions below

1
On

After a year, maybe I can help you.

See this PR that I made do django-jet:

https://github.com/geex-arts/django-jet/pull/329

It will pass the filters of the form field to the select2 element, which in it's turn will pass those filters to the django-jet, when showing the options. It's working perfectly to me.

Hope it helps.

0
On

In fact this is not an answer, but a comment to Paulo's PR.

With Paulo's code and Without filters I received an error (Django 2.2.1):

ERROR Internal Server Error: /jet/model_lookup/
Traceback (most recent call last):
  File "/home/mirek/dj/rian/ve_rian/lib/python3.7/site-packages/django/utils/datastructures.py", line 78, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'filters'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mirek/dj/rian/ve_rian/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/mirek/dj/rian/ve_rian/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/mirek/dj/rian/ve_rian/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/mirek/dj/rian/ve_rian/lib/python3.7/site-packages/django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "/home/mirek/dj/rian/isms/django-jet/jet/views.py", line 64, in model_lookup_view
    items, total = form.lookup()
  File "/home/mirek/dj/rian/isms/django-jet/jet/forms.py", line 135, in lookup
    qs.query.__dict__ = pickle.loads(codecs.decode(self.data['filters'].encode(), 'base64'))
  File "/home/mirek/dj/rian/ve_rian/lib/python3.7/site-packages/django/utils/datastructures.py", line 80, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'filters'

This error no longer occurs if I change your code in jet/forms.py:

def lookup(self):
    # patch: https://github.com/geex-arts/django-jet/pull/329/files https://stackoverflow.com/questions/45065632/making-django-jet-autocomplete-with-tabularinlines
    if 'filters' in self.data:
        qs = self.model_cls.objects.all()
        qs.query.__dict__ = pickle.loads(codecs.decode(self.data['filters'].encode(), 'base64'))
    else:
        qs = self.model_cls.objects

However I don't know if everything works well with the filter (I have not tested it, because I don't use the filter).