django-filer icon indicating copy to clipboard operation
django-filer copied to clipboard

ModelAdmin must define "search_fields",

Open ahcm opened this issue 1 year ago • 6 comments

Hi,

upgrading from Django 2 I get:

$ ../venv/bin/python webportal/manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
<class 'filer.admin.fileadmin.FileAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by FileAdmin.autocomplete_fields.
<class 'filer.admin.folderadmin.FolderAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by FolderAdmin.autocomplete_fields.
<class 'filer.admin.imageadmin.ImageAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by ImageAdmin.autocomplete_fields.

These seem to define search_fields though.

Thanks !

Best Andreas

ahcm avatar Jul 03 '24 01:07 ahcm

@ahcm I cannot reproduce this. Can you give some version info?

fsbraun avatar Jul 03 '24 17:07 fsbraun

I can fix it by adding a value to search_fields to ModelAdmin in django.contrib.admin.options. Changing search_fields = () to search_fields=('foo',).

Python 3.11.2 django-filer 3.1.3 django 5.0.6

ahcm avatar Jul 04 '24 21:07 ahcm

Any other packages updated that might interfere with the admin?

fsbraun avatar Jul 05 '24 11:07 fsbraun

Installed apps are plenty:

INSTALLED_APPS = [ # added below 1 line for the accounts app 'accounts.apps.AccountsConfig', # added below 1 line for the contact app 'contact.apps.ContactConfig', # added below 1 line for the observations app 'observations.apps.ObservationsConfig', # added below 1 line for the surveys app 'surveys.apps.SurveysConfig', # added below 1 line for the pages app 'pages.apps.PagesConfig', # added below 1 line for the map app 'map.apps.MapConfig', # added below 1 line for the lexicon app 'lexicon.apps.LexiconConfig', # added below 1 line for the wiki app 'wiki.apps.WikiConfig', # added below 1 line for the offline interface 'offline.apps.OfflineConfig', # added below 1 line for the news app 'news.apps.NewsConfig', # added below 1 line for the messenger app 'messenger.apps.MessengerConfig', # added below 1 line for the api interface 'api.apps.ApiConfig', # added below 1 line for newsletter app 'newsletter', # added below 5 lines for Django CMS 'django.contrib.sites', 'cms', 'menus', 'treebeard', 'djangocms_admin_style', # added below 6 lines for Django CMS 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'djangocms_text_ckeditor', 'djangocms_link', 'djangocms_file', 'djangocms_picture', 'djangocms_video', 'djangocms_googlemap', 'djangocms_snippet', 'djangocms_style', 'djangocms_column', 'djangocms_icon', # added below 15 lines for Django CMS Bootstrap 4 'djangocms_bootstrap4', 'djangocms_bootstrap4.contrib.bootstrap4_alerts', 'djangocms_bootstrap4.contrib.bootstrap4_badge', 'djangocms_bootstrap4.contrib.bootstrap4_card', 'djangocms_bootstrap4.contrib.bootstrap4_carousel', 'djangocms_bootstrap4.contrib.bootstrap4_collapse', 'djangocms_bootstrap4.contrib.bootstrap4_content', 'djangocms_bootstrap4.contrib.bootstrap4_grid', 'djangocms_bootstrap4.contrib.bootstrap4_jumbotron', 'djangocms_bootstrap4.contrib.bootstrap4_link', 'djangocms_bootstrap4.contrib.bootstrap4_listgroup', 'djangocms_bootstrap4.contrib.bootstrap4_media', 'djangocms_bootstrap4.contrib.bootstrap4_picture', 'djangocms_bootstrap4.contrib.bootstrap4_tabs', 'djangocms_bootstrap4.contrib.bootstrap4_utilities', # geo-django gis functionality 'django.contrib.gis', # added below 1 line to enable multiple selection https://github.com/goinnn/django-multiselectfield 'multiselectfield', # added below 1 line for crispy forms from Bootstrap https://github.com/django-crispy-forms/django-crispy-forms 'crispy_forms', # added below 1 line for adding captcha on the contact form captcha https://github.com/mbi/django-simple-captcha 'captcha', # added below 2 lines for REST API 'rest_framework', 'rest_framework.authtoken', # added below 1 line for newsletter subscription https://github.com/matthiask/django-newsletter-subscription 'newsletter_subscription', # added below 1 line for newsletter subscription https://github.com/matthiask/towel/ 'towel', # added below 1 line for pwa https://github.com/silviolleite/django-pwa 'pwa', # added below 14 lines for Django CMS 'sekizai', 'easy_thumbnails', 'filer', 'mptt', # added below 1 line for adding avatar https://github.com/grantmcconnaughey/django-avatar 'avatar',

ahcm avatar Jul 05 '24 17:07 ahcm

ERRORS: <class 'filer.admin.fileadmin.FileAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by FileAdmin.autocomplete_fields. <class 'filer.admin.folderadmin.FolderAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by FolderAdmin.autocomplete_fields. <class 'filer.admin.imageadmin.ImageAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by ImageAdmin.autocomplete_fields. <class 'filer.admin.permissionadmin.PermissionAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by PermissionAdmin.autocomplete_fields.

sasikumar1978k avatar Aug 09 '24 10:08 sasikumar1978k

Yes, filer does define them:

  • https://github.com/django-cms/django-filer/blob/cce13d2ac69dd88b600a6b7c956789b53ebbf017/filer/admin/fileadmin.py#L61
  • https://github.com/django-cms/django-filer/blob/cce13d2ac69dd88b600a6b7c956789b53ebbf017/filer/admin/folderadmin.py#L64
  • ImageAdmin does inherit it from FileAdmin
  • https://github.com/django-cms/django-filer/blob/cce13d2ac69dd88b600a6b7c956789b53ebbf017/filer/admin/permissionadmin.py#L15

Some other code in your projects might be tempering with this.

Have you tried to open a shell (./manage.py shell) and do this?

In[1]: from filer.adminfileadmin import FileAdmin
In[2]: FileAdmin.search_fields

fsbraun avatar Aug 09 '24 11:08 fsbraun

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 09 '24 00:11 stale[bot]

In case any one else has this error. My project has a custom User Model. I fixed this by adding search_fields to my customer User model admin. Essentially, this is not a problem with this project, but a field missing from my user model admin.

rzwink avatar Nov 25 '24 01:11 rzwink

@rzwink Thanks for the explanation!

fsbraun avatar Nov 25 '24 07:11 fsbraun

Hi. I am facing the same issue. If I add search_fields=('foo',) , it resolves the issue but it is not a good solution. Is there any other solutions to solve the issue?

anjanpa avatar Feb 23 '25 09:02 anjanpa

@anjanpa The only other solution I can think of is removing the auto complete if if user model's admin does not have search_fields defined.

Would you like to come up with a PR?

fsbraun avatar Feb 23 '25 10:02 fsbraun

HI @fsbraun Thank you for acknowledging the problem. The problem occurred because I defined custom user model and didn't define search fields in the admin.py.

When I added search_fields in admin.py:

from django.contrib import admin from .models import User @admin.register(User) class UserAdmin(admin.ModelAdmin): search_fields = ["email"]

This solved my problem.

anjanpa avatar Feb 23 '25 11:02 anjanpa

I'm also facing the same error.

> python manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
<class 'filer.admin.fileadmin.FileAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by FileAdmin.autocomplete_fields.
<class 'filer.admin.folderadmin.FolderAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by FolderAdmin.autocomplete_fields.
<class 'filer.admin.imageadmin.ImageAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by ImageAdmin.autocomplete_fields.
<class 'filer.admin.permissionadmin.PermissionAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by PermissionAdmin.autocomplete_fields.

I just added following in INSTALLED_APPS section

    # Third-party App
    'mptt',
    'easy_thumbnails',
    'filer',
> pip freeze
asgiref==3.8.1
chardet==5.2.0
cssselect2==0.7.0
Django==5.1.6
django-filer==3.3.1
django-js-asset==3.0.1
django-mptt==0.16.0
django-polymorphic==3.1.0
easy-thumbnails==2.10
lxml==5.3.1
pillow==11.1.0
reportlab==4.3.1
setuptools==75.8.0
sqlparse==0.5.3
svglib==1.5.1
tinycss2==1.4.0
webencodings==0.5.1

jainj2305 avatar Feb 25 '25 13:02 jainj2305

in your admin.py file did you create useradmin(admin.ModelAdmin)

Also did you add search fields like this @admin.register(User) class UserAdmin(admin.ModelAdmin): search_fields = ["email"]

also, the user model I used is a custom model

anjanpa avatar Feb 25 '25 13:02 anjanpa

Great, Thanks @anjanpa , Following fix, fixed this issue for me.

@admin.register(CustomUser)
class UserAdmin(admin.ModelAdmin):
    search_fields = ["email"]

This means if there is custom user, then we need to define search_fields for custom user.

jainj2305 avatar Feb 25 '25 13:02 jainj2305

no when you are using filer , it uses auto complete fields. And for autocomplete fields, search fields are necessary in the corresponding model. That's why we need to use ModelAdmin and write search fields.

anjanpa avatar Feb 25 '25 14:02 anjanpa

What do you think about doing something like this in FileAdmin to handle User admins without search_fields defined? Or even futher simplify it with just an error that User model admin needs search_fields defined.

from django.contrib import admin
from django.contrib.auth import get_user_model
from .models import File

User = get_user_model()

class FileAdmin(PrimitivePermissionAwareModelAdmin):
    list_display = ('label',)
    list_per_page = 10
    search_fields = ['name', 'original_filename', 'sha1', 'description']
    readonly_fields = ('sha1', 'display_canonical')

    def __init__(self, model, admin_site):
        super().__init__(model, admin_site)
        
        # Check if the User model has search_fields defined in its admin
        user_admin = admin.site._registry.get(User)
        
        if user_admin and hasattr(user_admin, 'search_fields') and user_admin.search_fields:
            # If search_fields are defined, use autocomplete_fields for 'owner'
            self.autocomplete_fields = ['owner']
        else:
            # If not defined, set a sensible default for User model (e.g., username and email)
            if not user_admin:
                # Register a minimal admin for the User model with default search fields
                @admin.register(User)
                class DefaultUserAdmin(admin.ModelAdmin):
                    search_fields = ['username', 'email']
            elif hasattr(user_admin, 'search_fields'):
                # Set default search_fields if the User model admin is already registered
                user_admin.search_fields = ['username', 'email']
            
            self.autocomplete_fields = ['owner']

admin.site.register(File, FileAdmin)

rzwink avatar Feb 25 '25 14:02 rzwink