Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support django-grappelli #84

Open
abhakhi opened this issue Aug 16, 2022 · 2 comments
Open

Support django-grappelli #84

abhakhi opened this issue Aug 16, 2022 · 2 comments

Comments

@abhakhi
Copy link

abhakhi commented Aug 16, 2022

With django-grappelli, the date fields show up as edit boxes. While they work, would be better if date picker fields are used.

Might be duplicate of #55.

@silentsokolov
Copy link
Owner

Grappelli doesn't support #35 #14 #12

You are welcome to help with PR.

@jrobichaud
Copy link

jrobichaud commented Feb 29, 2024

I managed to have django-admin-rangefilter working with grappelli in my project. I hope it can help anyone wanting to finish the job.

image

I created an app rangefilter_grappelli to store all the hacks.

It still has css issues but at least it works.

app structure

rangefilter_grappelli/
rangefilter_grappelli/templates/admin/change_list.html
rangefilter_grappelli/templates/admin/change_list_filter_sidebar.html
rangefilter_grappelli/templatetags/__init__.py
rangefilter_grappelli/templatetags/grp_tags_overrides.py
rangefilter_grappelli/__init__.py
rangefilter_grappelli/apps.py

code

settings.py

Add rangefilter_grappelli in your project's django settings, make sure to add it before grappelli.

INSTALLED_APPS = [
    # ...
    "rangefilter_grappelli",  # <- make sure it is before grappelli or the template overrides will not work
    "rangefilter",
    "grappelli",
    "django.contrib.admin",
    # ...
]

rangefilter_grappelli/templatetags/grp_tags_overrides.py:

This script overrides admin_list_filter template tag contained in grappelli/templatetags/grp_tags.py.

This implementation detects first if it is a template from rangefilter and will use rangefilter's code instead of grappelli's when applicable.

from django.apps import apps

try:
    apps.get_app_config("grappelli")
except LookupError:
    pass
else:
    from django.contrib.admin.templatetags.admin_list import (
        admin_list_filter as __django_admin_list_filter,
    )
    from grappelli.templatetags.grp_tags import (
        admin_list_filter as __grappelli_admin_list_filter,
        register,
    )

    @register.simple_tag
    def admin_list_filter(cl, spec):
        template_path = spec.template
        if "rangefilter" in template_path:
            return __django_admin_list_filter(cl, spec)
        return __grappelli_admin_list_filter(cl, spec)

rangefilter_grappelli/apps.py

Just a basic app config, no magic here.

from django.apps import AppConfig


class RangefilterGrappelliConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "rangefilter_grappelli"

templates

The templates override their respective grappelli templates to force the date time pickers to be initialized. Otherwise the picker buttons will not appear unless list_editable is used.

rangefilter_grappelli/templates/admin/change_list.html

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

{% block javascripts %}
{{ block.super }}
<script type="text/javascript" charset="utf-8">
    (function($) {
        $(document).ready(function() {
            grappelli.initDateAndTimePicker();
        });
    })(grp.jQuery);
</script>
{% endblock %}

rangefilter_grappelli/templates/admin/change_list_filter_sidebar.html

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

{% block javascripts %}
{{ block.super }}
<script type="text/javascript" charset="utf-8">
    (function($) {
        $(document).ready(function() {
            grappelli.initDateAndTimePicker();
        });
    })(grp.jQuery);
</script>
{% endblock %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants