Django, how to generate an admin panel without models?

心已入冬 提交于 2019-12-03 15:59:36

I think there might be a simpler way than writing custom ORMS to get the admin integration you want. I used it in an app that allows managing Webfaction email accounts via their Control Panel API.

Take a look at models.py, admin.py and urls.py here: django-webfaction

To create an entry on the admin index page use a dummy model that has managed=False

Register that model with the 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. 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.

you can "mock" some class so it look like a model but it does proxy to your APIs

f.e.

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

The django ORM has a pluggable backent, which means that you can write a backend for things that aren't RDBMSes. 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.

Otherwise, you could bypass the ORM altogether, and write the forms manually for the access you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!