Opened 19 years ago

Closed 18 years ago

Last modified 17 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: no UI/UX: no

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@…> 19 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@…> 19 years ago.
Patch against r1217 to implement callables for extra_lookup_kwargs (fixed)

Download all attachments as: .zip

Change History (13)

comment:1 by hugo, 19 years ago

Resolution: duplicate
Status: newclosed

Duplicate to #667

comment:2 by Tom Tobin <korpios@…>, 19 years ago

Resolution: duplicate
Status: closedreopened

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.

by Tom Tobin <korpios@…>, 19 years ago

Patch against r1217 to implement callables for extra_lookup_kwargs

comment:3 by Tom Tobin <korpios@…>, 19 years ago

Added a patch to implement the behavior in question.

comment:4 by hugo, 19 years ago

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.

by Tom Tobin <korpios@…>, 19 years ago

Patch against r1217 to implement callables for extra_lookup_kwargs (fixed)

comment:5 by Tom Tobin <korpios@…>, 19 years ago

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 by hugo, 19 years ago

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 by ian@…, 19 years ago

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 by Tom Tobin <korpios@…>, 19 years ago

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

comment:9 by hugo, 19 years ago

Summary: Allow callables in extra_lookup_kwargs of generic views[patch] Allow callables in extra_lookup_kwargs of generic views

comment:10 by Adrian Holovaty, 18 years ago

Resolution: fixed
Status: reopenedclosed

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

comment:11 by korpios, 17 years ago

Reporter: changed from Tom Tobin <korpios@…> to Tom Tobin <korpios@…>
Note: See TracTickets for help on using tickets.
Back to Top