Ticket #3520: manager.diff

File manager.diff, 1.2 KB (added by Simon Litchfield <simon@…>, 8 years ago)
  • django/db/models/manager.py

     
    8484    def complex_filter(self, *args, **kwargs):
    8585        return self.get_query_set().complex_filter(*args, **kwargs)
    8686
     87    def search_filter(self, lookups, data, **kwargs):
     88        """Runs filter using field lookups and values from supplied dicts.
     89        For example, running search_filter with --
     90            lookups = {'myfield': 'myfield', 'mytext': 'mytext__startswith', 'mydate': 'mydata__lte'}
     91            data = {'myfield': 'somevalue', 'mytext': 'h', 'mydate': ''}
     92        Would be the same as --
     93            filter(myfield='somevalue', mytext__startswith='h')
     94        Skips empty values. Useful for searching against form or request data.
     95        """
     96        sf_args = {}
     97        for item in lookups:
     98            if data.has_key(item):
     99                if data[item] != '' and data[item] != None:
     100                    sf_args[lookups[item]] = data[item]
     101        sf_args.update(kwargs)
     102        return self.get_query_set().filter(**sf_args)
     103
    87104    def exclude(self, *args, **kwargs):
    88105        return self.get_query_set().exclude(*args, **kwargs)
Back to Top