简体   繁体   English

Django,如何生成没有模型的管理面板?

[英]Django, how to generate an admin panel without models?

I'm building a rather large project, that basically consists of this: 我正在构建一个相当大的项目,基本上由以下内容组成:

Server 1: Ice based services. 服务器1:基于冰的服务。 Glacier2 for session handling. Glacier2用于会话处理。 Firewall allowing access to Glacier2. 防火墙允许访问Glacier2。

Server 2: Web interface (read, public) for Ice services via Glacier2. 服务器2:通过Glacier2为Ice服务提供的Web界面(读取,公共)。 Admin interface for Ice services via Glacier 2. 通过Glacier 2提供Ice服务的管理界面。

The point I'm concerned with is the web interface. 我关心的是网络界面。 I want to use Django, because it's both written in python and has that incredibly useful automatic admin panel generator. 我想使用Django,因为它都是用python编写的,并且具有非常有用的自动管理面板生成器。

The web interface doesn't access any database. Web界面不访问任何数据库。 It connects to an Ice service on Server #1 via the Glacier2 router and uses the API exposed by those services to manipulate data. 它通过Glacier2路由器连接到Server#1上的Ice服务,并使用这些服务公开的API来操作数据。

And as you probably know, the admin generation in Django depends on the use of Django's ORM; 正如您可能知道的那样,Django中的admin生成依赖于Django的ORM的使用; which I'm not using since I have no database to access. 我没有使用,因为我没有数据库可以访问。

So I need to generate the admin panel, but, instead of having an standard data access like the ORM normally does, I need to intercept any "db-access" calls and transform them into Ice service calls, and then take the service's output (if any), transform it into whatever the ORM normally returns and return control to Django. 所以我需要生成管理面板,但是,我不需要像ORM那样进行标准数据访问,而是需要拦截任何“db-access”调用并将它们转换为Ice服务调用,然后获取服务的输出(如果有的话,将其转换为ORM通常返回的任何内容并将控制权返回给Django。

Anyone knows how I could do this? 谁知道我怎么能这样做? what would I need to subclass? 我需要什么子类? Any specific ideas? 任何具体的想法?

Thanks for your time. 谢谢你的时间。

I think there might be a simpler way than writing custom ORMS to get the admin integration you want. 我认为可能有一种比编写自定义ORMS更简单的方法来获得所需的管理集成。 I used it in an app that allows managing Webfaction email accounts via their Control Panel API. 我在一个允许通过其控制面板API管理Webfaction电子邮件帐户的应用程序中使用它。

Take a look at models.py, admin.py and urls.py here: django-webfaction 看看models.py,admin.py和urls.py这里: Django的webfaction

To create an entry on the admin index page use a dummy model that has managed=False 要在管理索引页面上创建条目,请使用managed = False的虚拟模型

Register that model with the admin. 使用admin注册该模型。

You can then intercept the admin urls and direct them to your own views. 然后,您可以拦截管理员网址并将其引导至您自己的视图。

This makes sense if the add/edit/delete actions the admin provides make sense for your app. 如果管理员提供的添加/编辑/删除操作对您的应用有意义,这是有意义的。 Otherwise you are better off overriding the admin index or changelist templates to include your own custom actions 否则,最好覆盖管理索引或更改列表模板以包含您自己的自定义操作

The real power of the contrib.admin is django Forms . contrib.admin的真正力量是django Forms In essence, the admin tool is basically auto-generating a Form to match a Model with a little bit of urls.py routing thrown in. In the end it would probably just be easier to use django Forms apart from the admin tool. 从本质上讲,管理工具基本上是自动生成一个Form,以匹配一个带有一点urls.py路由的模型。最后,除了管理工具之外,使用django Forms可能更容易。

you can "mock" some class so it look like a model but it does proxy to your APIs 你可以“模拟”一些类,所以它看起来像一个模型,但它确实代理了你的API

fe FE

class QuerysetMock(object):
    def all():
        return call_to_your_api()
    [...]


class MetaMock(object):
     def fields():
         return fields_mock_objects..
     verbose_name = ''
     [...]

class ModelMock(object):
    _meta = MetaMock()
    objects = QuerysetMock()

admin.site.register(ModelMock)

This may work.. but you need to do a lot django.model compatible stuff 这可能有用..但你需要做很多django.model兼容的东西

The django ORM has a pluggable backent, which means that you can write a backend for things that aren't RDBMSes. django ORM有一个可插拔的后台,这意味着你可以为不是RDBMS的东西编写后端。 It's probably a rather large task, but a good place to get started is with Malcolm Tredinnick's talk from DjangoCon 2008, Inside the ORM . 这可能是一个相当大的任务,但是开始的好地方是Malcolm Tredinnick在DjangoCon 2008, ORM内部的演讲。

Otherwise, you could bypass the ORM altogether, and write the forms manually for the access you need. 否则,您可以完全绕过ORM,并手动编写表单以进行所需的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM