I have a simple filter that takes input based upon the url.

urlpatterns = patterns('ahrlty.listings.views',
    (r'^(?P<listing_type>.*)$', 'index'),
)

In the view I do a simple validation against a tuple in my listings
model that I use for choices in a field.

    if dict(Listing.listing_types).has_key(listing_type) == False:
        listing_type = None

And finally I get my data filtering on the listing_type if it exists,
otherwise I just want all the data. (Instead of none of it.)

    if listing_type:
        data = Listing.objects.filter(is_published = True,
listing_type = listing_type)
    else:
        data = Listing.objects.filter(is_published = True)

That last if statement works for this situation but I can see a
situation where I'm passing a few optional arguments in and an if
statement would get unwieldy.

I was thinking of chaining the filters for each optional field.

    data = Listing.objects.filter(is_published = True)
    if listing_type:
        data = data.filter(listing_type = listing_type)

Is there a better way to construct the filter?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to