Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: List of valid field lookup operators

Does Django have an accessible list of all valid field lookup operators (those that are used by the QuerySet API, e.g. 'contains', 'in', 'lt', etc)?

Thanks

EDIT: For clarification, I mean an in-code list that I can import so, for example, I can check if a given string matches a valid operator.

like image 416
oogles Avatar asked Dec 22 '22 16:12

oogles


2 Answers

After searching the source for the operators, it lives in django.db.models.sql.constants.QUERY_TERMS.

A dictionary with lookup strings mapped to None.

'exact' in QUERY_TERMS

Thanks for this! Never would have gone looking, but I could definitely use this.

like image 133
Yuji 'Tomita' Tomita Avatar answered Jan 04 '23 05:01

Yuji 'Tomita' Tomita


As of Django 2.1, django.db.models.sql.constants.QUERY_TERMS constant is removed. Per the release notes, Django recommends the get_lookups() method of the Lookup Registration API as an alternative.

For a given field on a model, it can be accessed via:

MyModel._meta.get_field('my_field').get_lookups()
like image 30
oogles Avatar answered Jan 04 '23 05:01

oogles