Code

Opened 8 years ago

Closed 7 years ago

#2544 closed enhancement (invalid)

Pass variables from url handler to the query set of a generic view

Reported by: jon@… Owned by: nobody
Component: Generic views Version:
Severity: normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by adrian)

If would be nice if you could pass variables from the url regexp in urls.py to the filter of a query set when using generic views.

(r'^/?(?P<person>\w+)/$', 'django.views.generic.list_detail.object_list', {'queryset': Item.objects.filter(name__name__iexact=person))

Currently, a statement similar to the following will fail with a NameError similar to the below:

NameError at /dave/
name 'person' is not defined
Request Method: GET
Request URL:    http://localhost:8000/dave/
Exception Type: NameError
Exception Value:        name 'dave' is not defined

The ideal behaviour would be that the filter function would be able to accept values from the url dispatcher.

This ticket arises from http://groups.google.com/group/django-users/browse_thread/thread/40c34ae87492d4dd/67cabff7abbbabe5#67cabff7abbbabe5 this discussion on django-users.

Attachments (0)

Change History (3)

comment:1 Changed 8 years ago by adrian

  • Description modified (diff)

Maybe this?

queryset_func = lambda kwargs: Item.objects.filter(name__name__iexact=kwargs['person'])

(r'^/?(?P<person>\w+)/$', 'django.views.generic.list_detail.object_list', {'queryset_func': queryset_func})

The generic view would call queryset_func(kwargs) at runtime, with kwargs being the dictionary of parameters captured from the URL.

comment:2 Changed 7 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 7 years ago by ubernostrum

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

The solution here is to write a wrapper around the generic view.

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.