0

I have different apps in django as:

First APP:
  First APP model1
  First APP model2
SECOND APP:
  SECOND APP model1
  SECOND APP model2

Actually there are a lot of apps and a lot of models, I was wondering if I could somehow use search_fields in the http://localhost:8000/admin/ URL i.e. at the home of the Django admin site.

Django admin

As each app its own admin file, where I register them. So in this case where do I place search_fields?

Aashish Gahlawat
  • 409
  • 1
  • 7
  • 25

2 Answers2

1

You can use django-admin-searchbar

NOTE : I didn't used this pluggin yet

JPG
  • 82,442
  • 19
  • 127
  • 206
  • is this a third party library? Isn't there anything within django? – Aashish Gahlawat Aug 09 '18 at 06:53
  • Here we are extending the admin template, I thought if I could add it via django only – Aashish Gahlawat Aug 09 '18 at 06:55
  • I don't have any experience with *Django app console* modification. But I do have something as specified/asked [here](https://stackoverflow.com/questions/28512710/how-to-add-custom-search-box-in-django-admin) – JPG Aug 09 '18 at 06:59
0

I didn't manage to add a search bar directly into admin home page however I did create a page which list all apps (according to the permissions of the connected user) and if you click on an application link, it sends you to the app home page. I added a button to the admin home page to open this web page containing the filter.

xxxx_project/templates/list_apps.html :

{% extends "admin/base.html" %}

{% block content %}
  <div class="container">

    <h1>Applications</h1>

    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Filter..." title="Filter on applications"><br><br>

    <ul id='myUL' style='list-style-type:none'>
        {% for app in app_list %}
            {% if app.label in perms %}
                <li><a href="{% url 'admin:app_list' app.label %}" style='text-decoration:none; color:#333'>{{ app.name }}</a></li>
            {% endif %}
        {% endfor %}
    </ul>

    <script>
        function myFunction() {
            var input, filter, ul, li, a, i;
            input = document.getElementById("myInput");
            filter = input.value.toUpperCase();
            ul = document.getElementById("myUL");
            li = ul.getElementsByTagName("li");
            for (i = 0; i < li.length; i++) {
                a = li[i].getElementsByTagName("a")[0];
                if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
                    li[i].style.display = "";
                } else {
                    li[i].style.display = "none";
                }
            }
        }
    </script>

{% endblock %}

xxxx_project/urls.py :

from .views import list_apps

urlpatterns = [
    ....
    path('apps/', list_apps, name='list_apps'),
]

xxxx_project/views.py :

def list_apps(request):
    installed_apps = apps.get_app_configs()
    exclude_labels = ['admin', 'auth', 'contenttypes', 'sessions', 'messages', 'staticfiles']
    app_list = [{'name': app.verbose_name, 'label': app.label} for app in installed_apps if app.label not in exclude_labels]
    return render(request, 'list_apps.html', {'app_list': app_list})

Then, go to url "xxxxxx/apps/" :

enter image description here

It allows me to filter on apps names :

enter image description here

Pozinux
  • 964
  • 2
  • 10
  • 22