Code

Opened 7 years ago

Closed 7 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: master
Severity: Keywords: search form filter request post get field lookup dict
Cc: SmileyChris Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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@…> 7 years ago.

Download all attachments as: .zip

Change History (4)

Changed 7 years ago by Simon Litchfield <simon@…>

comment:1 Changed 7 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

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

comment:2 Changed 7 years ago by Simon Litchfield <simon@…>

  • Cc SmileyChris 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 Changed 7 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.