Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to create a custom admin view without a model behind it

Tags:

django

admin

I have an object which I want to use under admin instead of a model which inherits models.Model. If I make it inherit models.Model, this object will create a table in the database which i don't want. I only want this object to stay in memory.

One solution I have come with help from the nice people at stack overflow is I create admin views, register these custom views via a modelAdmin ( admin.site.register() ) under admin.py and use this model-like object as dynamic data storage (in memory).

Since this model like object doesn't inherit from models.Model, admin.site.register() (under admin.py) doesnt accept it and shows a 'type' object is not iterable" error when I try to access it in the browser.

like image 236
sysasa Avatar asked Nov 10 '10 20:11

sysasa


People also ask

How do I automatically register all models in Django admin?

To automate this process, we can programmatically fetch all the models in the project and register them with the admin interface. Open admin.py file and add this code to it. This will fetch all the models in all apps and registers them with the admin interface.

What is model admin in Django?

It is used to create the form presented on both the add/change pages. You can easily provide your own ModelForm to override any default form behavior on the add/change pages. Alternatively, you can customize the default form rather than specifying an entirely new one by using the ModelAdmin. get_form() method.

How do I add an administrator to a model?

You can check the admin.py file in your project app directory. If it is not there, just create one. Edit admin.py and add below lines of code to register model for admin dashboard. Here, we are showing all the model fields in the admin site.


2 Answers

hmmm. Thanks for your help everyone. The solution I have come up ( with your help ofcourse :) is as follows:

I have two custom templates:

   my_model_list.html    my_model_detail.html 

Under views.py:

class MyModel(object):     # ... Access other models     # ... process / normalise data      # ... store data  @staff_member_required def my_model_list_view(request) #show list of all objects     #. . . create objects of MyModel . . .     #. . . call their processing methods . . .     #. . . store in context variable . . .      r = render_to_response('admin/myapp/my_model_list.html', context, RequestContext(request))     return HttpResponse(r)  @staff_member_required def my_model_detail_view(request, row_id) # Shows one row (all values in the object) in detail          #. . . create object of MyModel . . .     #. . . call it's methods . . .     #. . . store in context variable . . .      r = render_to_response('admin/myapp/my_model_detail.html', context, RequestContext(request))     return HttpResponse(r) 

Under the main django urls.py:

urlpatterns = patterns(      '',     (r'^admin/myapp/mymodel/$', my_model_list_view),     (r'^admin/myapp/mymodel/(\d+)/$', my_model_detail_view),     ( r'^admin/', include( admin.site.urls ) ) ) 
like image 96
sysasa Avatar answered Sep 19 '22 13:09

sysasa


You can add your views directly to the AdminSite object, rather than to any particular ModelAdmin subclass which you then register.

The default AdminSite is accessed via django.contrib.admin.site, which is what you call register and autodiscover on. Instead of using this, you could create your own subclass and add your own views to it, and then register your models against that rather than the default one.

like image 20
Daniel Roseman Avatar answered Sep 19 '22 13:09

Daniel Roseman