djangodjango-admindjango-grappelli

django admin statistical bar/graph.


How to show a very simple statistical bar/graph bars on the right looking for a django package for easy implement i'd like it to be inside admin change page or model shown as an admin-inline Thanks


Solution

  • You don't need any package to do this. You can show such graph with the two <div> and some CSS styling:

    class StatsAdmin(admin.ModelAdmin):
    
        list_display = ('name', 'total', 'passed', 'failed', 'pass_fail')
    
        def pass_fail(self, obj):
    
            if not obj.total:
                return ('<div style="width: 100px; height: 10px; border:'
                                    '1px solid black"></div>')
    
            percent_passed = int(obj.passed * 100.0 / obj.total)
            return ('<div style="width: 100px; height: 10px; '
                                'border: 1px solid black; background: red">'
                        '<div style="width: %spx; height: 10px; '
                                    'background: green"></div>'
                    '</div>' % percent_passed)
    
        pass_fail.allow_tags = True
        pass_fail.short_description = 'Pass / Fail'
    

    Exmple screenshot