Custom Text filter field in django admin site.

244 views
Skip to first unread message

Ayush Bisht

unread,
May 31, 2021, 6:28:06 AM5/31/21
to Django users
# admin.py

class InputFilter(admin.SimpleListFilter):
    template = 'patient/input_filter.html'

    def lookups(self, request, model_admin):
        # Dummy, required to show the filter.
        return ((),)

class PatientProfilePastDataFilter(InputFilter):
    parameter_name = 'patient'
    title = _('Past Record')

    def lookups(self, request, model_admin):
        return ( (),)

    def queryset(self, request, queryset):       
        return queryset.filter(admitted_on__lte = request.GET.get("from") , admitted_on__gte           = request.GET.get("to")  )

# patient/input_filter.html
{% load i18n %} 

<h3>{% blocktrans with filter_title=title %} By {{ filter_title }} {% endblocktrans %}</h3>
<ul>
    <li>
        {% with choices.0 as all_choice %}
        <form method="GET" action="">

            <!-- {% for k, v in all_choice.query_parts %}
            <input type="hidden" name="{{ k }}" value="{{ v }}" />
            {% endfor %} -->

            <input  type="date"
                    value="{{ spec.value|default_if_none:'' }}"
                    name="from"/>

            <input  type="date"
            value="{{ spec.value|default_if_none:'' }}"
            name="to"/>

            <button type="submit"  >filter</button>
            {% if not all_choice.selected %}
                <strong><a href="{{ all_choice.query_string }}">x {% trans 'Remove' %}</a></strong>
            {% endif %}

        </form>
        {% endwith %}
    </li>
</ul>

 # .................................................................................


I want to add a custom text filter in django admin site... with 2 input fields. Is there a way to do this.. if so then please suggest me,  how can I add a custom text field in filter section.

Also, Thanks for the  respond on previous query... 

Reply all
Reply to author
Forward
0 new messages