如何正确设置djangocms_blog中的BLOG_ADMIN_POST_FIELDSET_FILTER?
解决djangocms_blog中BLOG_ADMIN_POST_FIELDSET_FILTER配置的AppRegistryNotReady错误
问题根源
直接在settings.py中导入admin.py中的函数会触发AppRegistryNotReady错误,因为Django加载settings时,所有应用还未完成注册,而admin.py依赖于已初始化的CMS应用模块。同时当前版本的djangocms_blog仅接受可调用对象作为该配置值,字符串路径方式无效。
解决方案
方案1:将过滤函数移至无依赖的工具模块
- 在你的应用下创建
utils.py文件,将过滤函数迁移至此:
# myapp/utils.py def custom_fieldset_filter_function(fsets, request, obj=None): if request.user.groups.filter(name='Editor').exists(): fsets[1][1]['fields'][0].append('author') # 为Editor组用户添加author字段 return fsets
- 在
settings.py中导入该函数并配置:
# settings.py from myapp.utils import custom_fieldset_filter_function BLOG_ADMIN_POST_FIELDSET_FILTER = custom_fieldset_filter_function
此方法避免了直接导入依赖应用注册的admin模块,从根源上解决加载顺序问题。
方案2:使用延迟导入的可调用包装类
如果不想移动函数,可通过包装类实现延迟导入,确保仅在实际调用过滤函数时才加载admin模块:
# settings.py class LazyFieldsetFilter: def __call__(self, fsets, request, obj=None): from myapp.admin import custom_fieldset_filter_function return custom_fieldset_filter_function(fsets, request, obj) BLOG_ADMIN_POST_FIELDSET_FILTER = LazyFieldsetFilter()
当djangocms_blog后台需要处理字段集时,才会触发__call__方法,此时Django应用已完成注册,不会报错。
内容的提问来源于stack exchange,提问作者sodimel
相关产品推荐
相关产品推荐

