Search code examples
djangowagtail

ImportError when setting wagtail development environment : "cannot import name 'Query' from 'wagtail.search.models'"


I'm currently working on a fork of Wagtail (version 6.0a0) and attempting to set up a development environment for contributing to the project. I've followed the standard steps for setting up the project but have encountered an ImportError when running migrations for a Wagtail-based website.

Steps I've Taken:

  • Cloned the forked repository locally.
  • Activated a Python virtual environment for my work.
  • Installed the cloned project using "pip install -e .[testing,docs] -U."
  • Executed "npm ci" and "npm run build" for JavaScript dependencies.
  • Created a new Wagtail website in a separate directory.
  • Ran "python manage.py migrate," and encountered the ImportError.

Error Message:
Traceback (most recent call last):
    File "path/to/manage.py", line 10, in
        ...
        from wagtail.search.models import Query
ImportError: cannot import name 'Query' from 'wagtail.search.models'

Additional Information:

  • The released version of Wagtail (version 5.2a0) installs successfully using "pip install wagtail" without any issues.
  • My requirements file specifies Django version 4.2, but Django version 4.0.10 is installed in my Python virtual environment.(this version was installed with the cloned wagtail project)
  • The requirements.txt file in the website generated by Wagtail specifies the following: Django>=4.2,<4.3 // wagtail==5.2a0

I at first thought this might be a dependency versions problem because the released version works fine with the Django 4.2 version, so I updated Django to 4.2 but the issue remained.

My questions:

  • What could be causing this ImportError when using my fork of Wagtail (version 6.0a0) that doesn't occur when using the released version (5.2a0)?
  • How to set the development environment to contribute to wagtail (I tried following the docs as shown but i cant set it right)

Solution

  • As noted in Rich's answer, the Query model (referenced in search/views.py in the initial project structure created by wagtail start) was moved from the wagtail.search module to wagtail.contrib.search_promotions in Wagtail 5.0. The old model was left in place until work began on Wagtail 6.0 a week or two ago.

    Leaving the original import in place in the default project structure was a mistake, and I've now submitted a fix for this: https://github.com/wagtail/wagtail/pull/11190. Once this is fix is merged, bringing up a new project will work as intended.

    In the meantime, you can patch the created project yourself - either change the import in search/views.py as Rich suggests, or remove it along with these two lines in the search function:

        query = Query.get(search_query)
        query.add_hit()
    

    Alternatively, I'd recommend using bakerydemo as a test project for developing Wagtail against, rather than creating a new project from scratch - the relevant fix was applied to bakerydemo a while back.