【问题标题】:Showing all models in Django admin panel in development在开发中的 Django 管理面板中显示所有模型
【发布时间】:2018-04-22 08:42:30
【问题描述】:

在开发过程中,我经常需要从管理面板访问数据,但我不希望在 admin.py 中添加所有模型,因为我不希望在生产环境中访问它们。

有没有办法在开发环境的管理面板中显示所有模型并在生产中自动隐藏(部分)它们?

【问题讨论】:

    标签: python django django-admin


    【解决方案1】:

    我想就这么简单:

    # my_app/admin.py
    
    from django.contrib import admin
    from django.conf import settings
    
    from .models import MyModel, AnotherModel
    
    class MyModelAdmin(admin.ModelAdmin):
        pass
    
    class AnotherModelAdmin(admin.ModelAdmin):
        pass
    
    # conditional registration of models
    if settings.DEBUG:
        admin.register(MyModel, MyModelAdmin)
        admin.register(AnotherModel, AnotherModelAdmin)
    

    【讨论】:

      猜你喜欢
      • 2023-02-04
      • 2019-04-27
      • 2020-07-17
      • 1970-01-01
      • 2015-10-25
      • 2020-09-30
      • 2015-12-26
      • 2013-03-07
      • 1970-01-01
      相关资源
      最近更新 更多