def __call__(self, *args, **kwargs): """ Lookup custom helpers, otherwise keep old-style Q usage >>> Q(user__age__lte=7) <Q: (AND: ('user__age__lte', 7))> >>> Q.article.tags.overlap('holiday', 'x-mas') <Q: (AND: ('article__tags__overlap', ('holiday', 'x-mas')))> >>> tags = Q.article.tags >>> tags.overlap('holiday', 'x-mas') <Q: (AND: ('article__tags__overlap', ('holiday', 'x-mas')))> """ if self._parent: helper = self._helpers.get(self._name) if helper: return helper(self._parent.get_path(), *args, **kwargs) else: # create Q object based on full path if len(args) == 1: value = args[0] else: value = args return QNode(**{self.get_path(): value}) else: # just create usual Q object return QNode(**kwargs)
def in_list(self, lst): """ Filter by fields matching a given list >>> QFactory().user.id.in_list([1, 2, 3]) <Q: (AND: ('user__id__in', [1, 2, 3]))> """ return QNode(**{'{}__in'.format(self.get_path()): lst})
def __eq__(self, value): """ >>> Q.username == 'Bender Rodriguez' <Q: (AND: ('username', 'Bender Rodriguez'))> >>> Q.user.username == 'Bender Rodriguez' <Q: (AND: ('user__username', 'Bender Rodriguez'))> """ return QNode(**{self.get_path(): value})
def is_null(self, value=True): """ Filter by null (or not-null) fields >>> QFactory().user.favorite_movie.is_null() <Q: (AND: ('user__favorite_movie__isnull', True))> """ return QNode(**{'{}__isnull'.format(self.get_path()): value})
def __gt__(self, value): """ >>> QFactory().user.age > 7 <Q: (AND: ('user__age__gt', 7))> """ return QNode(**{'{}__gt'.format(self.get_path()): value})
def __ne__(self, value): """ >>> QFactory().user.username != 'Bender Rodriguez' <Q: (NOT (AND: ('user__username', 'Bender Rodriguez')))> """ return ~QNode(**{self.get_path(): value})