Opened 18 years ago

Closed 17 years ago

#3520 closed (wontfix)

[patch] New search_filter for default manager

Reported by: Simon Litchfield <simon@…> Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: search form filter request post get field lookup dict
Cc: Chris Beaven Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Simple convenience method to run filter() using field lookups and values from supplied dicts.

mysearch = {'myfield': 'myfield', 
           'mytext': 'mytext__startswith', 
           'mydate': 'mydata__lte',
    }
formdata = {'myfield': 'somevalue', 
            'mytext': 'h', 
            'mydate': ''
    }
results = MyObject.objects.search_filter(lookups=mysearch, data=formdata)

Would be exactly the same as --

filter(myfield='somevalue', mytext__startswith='h')

Skips empty values. Useful for searching against form or request data.

Attachments (1)

manager.diff (1.2 KB ) - added by Simon Litchfield <simon@…> 18 years ago.

Download all attachments as: .zip

Change History (4)

by Simon Litchfield <simon@…>, 18 years ago

Attachment: manager.diff added

comment:1 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedDesign decision needed

I don't know if this is really necessary. But maybe Simon will try and convince us some more ;)

comment:2 by Simon Litchfield <simon@…>, 18 years ago

Cc: Chris Beaven added

Well you're right Smiley, it's not necessary.

But just imagine showing it to a newbie user (like me), in a tutorial or something. It'd put a big smile on their face to make a form with newforms, and spit their form.clean_data straight into search_filter. Thats your typical 'advanced search' page done in minutes, and only a few lines of code! Sure you just write a loop to build and unpack the params to filter(), but your average new user probably won't think of that. It's only a few extra lines in manager.py.

More specific descriptive naming, like filter_nonblank(), filter_map(ignore_blank=True) or something, might help. Or, you could even whack it into filter(), to keep things more concise. Like filter(lookups={}, data={}), at the risk of conflicting with field names tho, maybe filter(searchlookups={}, searchdata={})..... Anyway over to you guys. Go easy on me I'm only new :-)

comment:3 by Jacob, 17 years ago

Resolution: wontfix
Status: newclosed

Chris is completely right: this is not necessary. New users will be better served in the long run by figuring out how to use kwargs.

Note: See TracTickets for help on using tickets.
Back to Top