18

I want to implement a report section in Django admin. This would mean adding a custom section in the admin homepage where instead of a list of models I would see a list of reports. I want to use Django's admin tables with filters, sorting, everything if possible.

What would be the "best" way of achieving this? I realize this is a "big" question so I'm not asking for code snippets necessarily, a summary of needed actions would be just fine :)

P.S. Be report I mean a "made up" model by custom queries (queryset or how it's called).

P.S.2 Maybe this question should be something like: How to use Django admin tables functionality in own admin view?

P.S.3 Or maybe there is a way of providing to the existing admin interface my own data. This way I don't have to do anything else. I just want to say instead of a model take this data and display it in a nice table which I can sort, filter etc etc.

Al Bundy
  • 707
  • 2
  • 5
  • 12
  • what do you mean by "list of reports"? Do you have some specific kind of report in mind? – Calvin Cheng Nov 03 '12 at 15:18
  • What do you mean with **reports**? You can prepare a custom page with django and make any kind of anything you want. But using default django filters in a such view and template is not possible. But you can easily write our filters. How to [skip admin index page and going to a specific one](http://stackoverflow.com/a/12424461/257972) – Mp0int Nov 03 '12 at 15:21
  • I mean you have now in the Django admin homepage the app name as the section's header (with blue background) and then the list of models of that app. I want a section with the Reports header and then a list of reports like Sales, Best Sellers... just about any table that doesn't have a model behind it but it's made up by some code. Instead of an actual model I would have a "made up" model behind. – Al Bundy Nov 03 '12 at 15:24
  • You can build "mock" models: http://stackoverflow.com/questions/1813637/django-how-to-generate-an-admin-panel-without-models – Al Bundy Nov 04 '12 at 18:44

1 Answers1

14

So you are attempting to add in new pages into the django admin.

This section explains to you exactly how you can do so - https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites

The basic idea is to add in new urls that you want in your urls.py as if you are adding urls for your "front end" pages. The key difference is that these new urls you are adding should start with ^admin/ and would look something like ^admin/my_special_link_in_admin and this url will point to your own custom view function at a location you so prefer.

E.g.

(r'^admin/my_special_link_in_admin/$', 'my_custom_admin_app.views.special_admin_page'),

So this is the way for complete customization. There's a very good tutorial which I refer to here - http://brandonkonkle.com/blog/2010/oct/4/django-admin-customization-examples/

In addition, if you don't want to do too much work, consider using Django Admin Plus - https://github.com/jsocol/django-adminplus

Or a django-admin-views - https://github.com/frankwiles/django-admin-views

Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
  • Yes, I want new pages but I don't want to use my own table templates and functionality. This is huge work! :) I want the nice Django admin to work for custom data also, not just model data :D – Al Bundy Nov 03 '12 at 15:38
  • Yup. I get it. Use https://github.com/jsocol/django-adminplus as mentioned in my updated answer. – Calvin Cheng Nov 03 '12 at 15:44
  • Will this adminplus take of my hands the need to write table sorting, for example? – Al Bundy Nov 03 '12 at 15:47
  • Yes. It is 100% compatible with standard django admin functionality. All it does - if you check out its templates here https://github.com/jsocol/django-adminplus/tree/master/adminplus/templates/adminplus - is to extend to use the standard django admin templates while permitting you to write your own queries in your custom view function, which you would need to register like this `admin.site.register_view('somepath', my_view)` – Calvin Cheng Nov 03 '12 at 15:49
  • I see. This would do it, but please check out my 3rd edit to the question. Can that be done? Would be alot more fast and elegant. – Al Bundy Nov 03 '12 at 15:52
  • I think you have to at the very least write your own custom query :-D Otherwise, how would the page know "what to show"? If your target data source isn't a django model, you will need to decide what data you want to show, no? – Calvin Cheng Nov 03 '12 at 15:55
  • Sorry for extending my question. Yes that is the only thing I want to do if possible. Write my own query, pass it to the Django admin as a "fake" model and let Django admin display it in a table. You have to agree that would be the nicest way :P – Al Bundy Nov 03 '12 at 15:57
  • Isn't that exactly what this example shows you? https://github.com/jsocol/django-adminplus#using-adminplus – Calvin Cheng Nov 03 '12 at 16:02
  • Well, I don't know, is it? I mean I want more than just writing a view. That view will create some tabular data and I need that data displayed with Django's admin table functionality? How do I do that? I mean why write code when it's already there. I just don't know how to plugin into it :) – Al Bundy Nov 03 '12 at 16:06