django-cms

How to set BLOG_ADMIN_POST_FIELDSET_FILTER in djangocms_blog?


I just came across this part of the djangocms_blog documentation that says:

You can add / remove / filter fields at runtime by defining a method on you custom admin and proving its name in BLOG_ADMIN_POST_FIELDSET_FILTER.

Function example:

def fieldset_filter_function(fsets, request, obj=None):
    if request.user.groups.filter(name='Editor').exists():
        fsets[1][1]['fields'][0].append('author')  # adding 'author' field if user is Editor
    return fsets

I don't know what to put inside this BLOG_ADMIN_POST_FIELDSET_FILTER setting.

I tried to set it to those 2 values, but it's not working:

BLOG_ADMIN_POST_FIELDSET_FILTER = "myapp.admin.custom_fieldset_filter_function"
BLOG_ADMIN_POST_FIELDSET_FILTER = "custom_fieldset_filter_function"

I came across the PR that added this features, and it seems to only accept a callable.

So I tried this:

from myapp.admin import custom_fieldset_filter_function
BLOG_ADMIN_POST_FIELDSET_FILTER = custom_fieldset_filter_function

But I got this answer from my dev server:

Traceback (most recent call last):
[...]
  File "/home/me/test-website/website/settings.py", line 254, in <module>
    from my_test_app.admin import custom_fieldset_filter_function
  File "/home/me/my-test-app/my_test_app/admin.py", line 2, in <module>
    import djangocms_blog.admin as blog_admin
  File "/home/me/test-website/.venv/lib/python3.10/site-packages/djangocms_blog/admin.py", line 4, in <module>
    from cms.admin.placeholderadmin import FrontendEditableAdminMixin, PlaceholderAdminMixin
[...]
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I don't know what to do in order to set this var to a callable.


Solution

  • I just realized that the error was triggered while trying to import djangocms_blog.admin, and that this import has nothing to do with my custom fieldset filter function, but is related to other things in my admin.py code.

    So I moved my custom_fieldset_filter_function outside of my admin.py file in order to stop the dependencies errors.

    It now sits happily in a file named update_fields.py, and everything's working as expected.