I registered a custom lookup base on this answer and official documentation.
# core/custom_lookup.py
from django.db.models import Lookup
class NotEqual(Lookup):
"""Missing != operator."""
lookup_name = 'ne'
def as_sql(self, *args):
lhs, lhs_params = self.process_lhs(*args)
rhs, rhs_params = self.process_rhs(*args)
return '{} <> {}'.format(lhs, rhs), lhs_params + rhs_params
I registered the custom lookup in my app config.
# core/apps.py
from django.apps import AppConfig
from django.db.models import Field
from .custom_lookup import NotEqual
class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core'
def ready(self):
Field.register_lookup(NotEqual)
Then i use it in the exclude method this way.
Customer.objects.all().filter(filter).exclude(private=True, user__ne=info.context.user).order_by('-created')
The idea here is to get all the customers where user is info.context.user (with private is False or True) OR user is NOT info.context.user but private is False but exclude customers where user is NOT info.context.user AND private is True
But whenever I play the query the error below is thrown
Related Field got invalid lookup: ne
Am I missing something on the registration of the lookup?