Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'QUERY_TERMS' on Django 2.1 #1561

Closed
bastbnl opened this issue Aug 1, 2018 · 10 comments
Closed

ImportError: cannot import name 'QUERY_TERMS' on Django 2.1 #1561

bastbnl opened this issue Aug 1, 2018 · 10 comments
Labels
django-update Changes for new releases of Django ready for release Candidate for inclusion in next release; may need small changes or help from a collaborator.

Comments

@bastbnl
Copy link

bastbnl commented Aug 1, 2018

Hi,

I just updated to Django 2.1 from Django 2.0.7 using django-tastypie==0.14.1 on python 3.5 and ran into this error:

...
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/var/www/brownpapersession/dev/brownpapersession/brownpapersession/urls.py", line 16, in <module>
    from tastypie.api import Api, NamespacedApi
  File "/var/www/brownpapersession/dev/env/lib/python3.5/site-packages/tastypie/api.py", line 11, in <module>
    from tastypie.resources import Resource
  File "/var/www/brownpapersession/dev/env/lib/python3.5/site-packages/tastypie/resources.py", line 32, in <module>
    from django.db.models.sql.constants import QUERY_TERMS
ImportError: cannot import name 'QUERY_TERMS'

I'll see if I can find more information on QUERY_TERMS

@bastbnl
Copy link
Author

bastbnl commented Aug 1, 2018

The message disappears after changing resource.py to this:

try:
    from django.db.models.sql.constants import QUERY_TERMS
except ImportError:
    # Django 2.1
   QUERY_TERMS = {
      'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in',
      'startswith', 'istartswith', 'endswith', 'iendswith', 'range', 'year',
      'month', 'day', 'week_day', 'hour', 'minute', 'second', 'isnull', 'search',
      'regex', 'iregex',
   }

@bastbnl
Copy link
Author

bastbnl commented Aug 1, 2018

I'll try to create a PR later on

@georgedorn
Copy link
Contributor

It usually takes a few weeks to implement full compatibility for a new release of Django.

For this one issue, QUERY_TERMS was probably moved; rather than re-implementing it, we should figure out the new right way to get a list of sql terms the ORM supports.

@lhopkins
Copy link

lhopkins commented Aug 2, 2018

Looks like QUERY_TERMS is removed.

@georgedorn
Copy link
Contributor

georgedorn commented Aug 6, 2018

This is turning into a fairly difficult problem to solve; tastypie was relying heavily on QUERY_TERMS to quickly pre-validate filters. There doesn't appear to be a graceful way to regenerate that set of operators without overhauling ModelResource.build_filters. This is long overdue, anyway; validating against a constant list of operators made it impossible to add more operators without monkey-patching, and the global list of operators meant it would allow nonsensical operations through for fields that make no sense, like a case-insensitive filter on an integer field.

The path forward is probably refactoring build_filters to retrieve the Django field and use the new lookups api to validate the operation.

@georgedorn
Copy link
Contributor

This was fixed in #1564 and merged into the django-2.1 branch. That branch isn't ready for release due to other test failures (see #1562), but I've confirmed that the remaining test failures are unrelated to QUERY_TERMS so this issue can be closed.

@georgedorn
Copy link
Contributor

To avoid further confusion, I'll leave this issue open until the rest of 2.1 support is implemented and merged to master.

@georgedorn georgedorn added ready for release Candidate for inclusion in next release; may need small changes or help from a collaborator. django-update Changes for new releases of Django labels Aug 14, 2018
@georgedorn
Copy link
Contributor

The test suite is now passing under Django 2.1. I'll release to PyPI on 9/3 if no major bugs are found before then.

This will also drop support for Django 1.8, which was EOL'd back in April.

@georgedorn
Copy link
Contributor

Released today.

@bastbnl
Copy link
Author

bastbnl commented Sep 4, 2018

Appears to be working fine, @georgedorn Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
django-update Changes for new releases of Django ready for release Candidate for inclusion in next release; may need small changes or help from a collaborator.
Projects
None yet
Development

No branches or pull requests

3 participants