是否可以创建一个自定义管理视图,而没有其后面的模型 [英] is it possible to create a custom admin view without a model behind it
问题描述
我从堆栈溢出的好人那里得到一个解决方案是创建管理员视图,通过modelAdmin(admin.site.register())注册这些自定义视图)在admin.py下,并使用这个类似模型的对象作为动态数据存储(在内存中)。
由于此类型的对象不会从models.Model继承, admin.site.register()(在admin.py下)不接受它,并显示一个类型对象不可迭代错误,当我尝试访问它在浏览器。
嗯,谢谢你们的帮助,我已经提出的解决方案(在你的帮助下)如下:
我有两个自定义模板:
my_model_list.html
my_model_detail.html
在views.py下:
class MyModel(object):
#...访问其他模型
#...进程/归一化数据
#...存储数据
@staff_member_required
def my_model_list_view(request)#show所有对象的列表
#。 。 。创建MyModel的对象。 。 。
#。 。 。称之为他们的处理方法。 。 。
#。 。 。存储在上下文变量中。 。 。
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)#详细显示一行(对象中的所有值)
#。 。 。创建MyModel的对象。 。 。
#。 。 。称之为方法。 。 。
#。 。 。存储在上下文变量中。 。 。
r = render_to_response('admin / myapp / my_model_detail.html',context,RequestContext(request))
return HttpResponse(r)
在主要的django urls.py下:
urlpatterns = patterns(
$ b(r'^ admin / myapp / mymodel / $',my_model_list_view),
(r'^ admin / myapp / mymodel /(\d +)/ $',my_model_detail_view) ,
(r'^ admin /',include(admin.site.urls))
)
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.
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 ) )
)
这篇关于是否可以创建一个自定义管理视图,而没有其后面的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!