Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#779 closed enhancement (fixed)

[patch] Allow callables in extra_lookup_kwargs of generic views

Reported by: Tom Tobin <korpios@…> Owned by: jacob
Component: Generic views Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

As with #195, it would be nice to allow callables in the extra_lookup_kwargs of generic views for similar reasons (e.g., a blog could pass in something like {'time_published__lte': datetime.now}).

Attachments (2)

extra_lookup_kwargs_callable_r1217.patch (580 bytes) - added by Tom Tobin <korpios@…> 8 years ago.
Patch against r1217 to implement callables for extra_lookup_kwargs
extra_lookup_kwargs_callable_r1217-fixed.patch (4.1 KB) - added by Tom Tobin <korpios@…> 8 years ago.
Patch against r1217 to implement callables for extra_lookup_kwargs (fixed)

Download all attachments as: .zip

Change History (13)

comment:1 Changed 8 years ago by hugo

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

Duplicate to #667

comment:2 Changed 8 years ago by Tom Tobin <korpios@…>

  • Resolution duplicate deleted
  • Status changed from closed to reopened

This isn't a duplicate of #667; it's along the lines of #195. I was talking about plain callables (with no args, like datetime.now), not callbacks.

Changed 8 years ago by Tom Tobin <korpios@…>

Patch against r1217 to implement callables for extra_lookup_kwargs

comment:3 Changed 8 years ago by Tom Tobin <korpios@…>

Added a patch to implement the behavior in question.

comment:4 Changed 8 years ago by hugo

I still think that #667 is a super-set of this - it asks for callables that get parameters (request and url parts). Your case could be done by using "lambda request, parts: datetime.datetime.now()" as the callable/. It would additionally allow to do processing based on request parameters or url parts - which your patch wouldn't allow. The more general case would be really helpful - think about ?q=searchword paramters that are turned into fieldcontains=searchword lookup kwargs.

Changed 8 years ago by Tom Tobin <korpios@…>

Patch against r1217 to implement callables for extra_lookup_kwargs (fixed)

comment:5 Changed 8 years ago by Tom Tobin <korpios@…>

Added a patch to implement originally discussed behavior for all generic views, as I realized my original patch only handled list_detail's object_list.

If you think the requested behavior in #667 is more useful, and can be applied equally well to this case, I'll take a shot at implementing that instead.

comment:6 Changed 8 years ago by hugo

I think both could be combined - #667 would be best implemented by asking wether extra_lookup_kwargs is a callable, your patch looks for items to be callables. If extra_lookup_kwargs is a callable, this could easily be used for your problem:

info_dict = {
  # ...
  'extra_lookup_kwargs': lambda request, **parms: {'time_published__lte': datetime.datetime.now()},
  # ...

That would do the same as your patch, only it would allow do even more. The difference in your patch would be to not check each item for a callable, but instead check the full extra_lookup_kwargs element for a callable and if it is one, call it with passing the request and the keyword parameters from the generic view to it.

My case would then be something like this:

def current_posts(request, **parms):
   kwargs = {
      'time_published__lte': datetime.datetime.now(),
   }
   if request.GET.has_key('q'):
      kwargs['title__contains'] = rquest.GET['q']
   return kwargs

info_dict = {
   # ...
   'extra_lookup_kwargs': current_posts,
   # ...
}

This would do the same as yours, but additionally would automatically filter posts by title if the users passes in a ?q=search+words parameter to the generic view URL.

comment:7 Changed 8 years ago by ian@…

I did something similar to this (based on hugo's work)
here
http://svn.zilbo.com/svn/django/common/filter/views/filter.py

it checks to see if the argument matches a field, and then adds the filter if it does.

comment:8 Changed 8 years ago by Tom Tobin <korpios@…>

See #667; patch with the callback behavior for extra_lookup_kwargs (but not extra_context yet).

comment:9 Changed 8 years ago by hugo

  • Summary changed from Allow callables in extra_lookup_kwargs of generic views to [patch] Allow callables in extra_lookup_kwargs of generic views

comment:10 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from reopened to closed

I'm closing this, as generic views no longer take extra_lookup_kwargs.

comment:11 Changed 7 years ago by korpios

  • Reporter changed from Tom Tobin <korpios@…> to Tom Tobin <korpios@…>

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.