#779 closed enhancement (fixed)
[patch] Allow callables in extra_lookup_kwargs of generic views
Reported by: | 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)
Change History (13)
comment:1 by , 19 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
comment:2 by , 19 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
by , 19 years ago
Attachment: | extra_lookup_kwargs_callable_r1217.patch added |
---|
Patch against r1217 to implement callables for extra_lookup_kwargs
comment:4 by , 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 , 19 years ago
Attachment: | extra_lookup_kwargs_callable_r1217-fixed.patch added |
---|
Patch against r1217 to implement callables for extra_lookup_kwargs (fixed)
comment:5 by , 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 , 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 , 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 , 19 years ago
See #667; patch with the callback behavior for extra_lookup_kwargs
(but not extra_context
yet).
comment:9 by , 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 , 18 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
I'm closing this, as generic views no longer take extra_lookup_kwargs
.
comment:11 by , 17 years ago
Reporter: | changed from | to
---|
Duplicate to #667