Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1041523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:21:56+00:00 2026-05-16T15:21:56+00:00

How can I do this? I only what to filter if the request is

  • 0

How can I do this?

I only what to filter if the request is set.

ex. if gender not set, the filter will be:
Test.objects.filter(categories=category, brands=brand)

def index(request):
    gender = request.GET.get('gender')
    category = request.GET.get('category')
    brand = request.GET.get('brand')

    Test.objects.filter(genders=gender, categories=category, brands=brand)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:21:57+00:00Added an answer on May 16, 2026 at 3:21 pm

    If you realize that filter conditions can be passed in as keyword arguments the solution becomes easier to visualize. For e.g. consider the snippet below. This is a verbose way of doing it:

    conditions = dict()
    
    for filter_key, form_key in (('genders',  'gender'), ('categories', 'category'), ('brands', 'brand')):
        value = request.GET.get(form_key, None)
        if value:
            conditions[filter_key] = value
    
    Test.objects.filter(**conditions)
    

    Of course another way of doing this would be to use a form rather than pick up the values directly from the GET request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does this smell? I have a few properties you can only set once. They
Can CouchDB's filtered replication be set to replicate not only the most recent version
Can anyone please tell me why this request keeps returning with failure. It only
Protected Means, we can access this member only in a deriving class, and internal
I can only assume this is a bug. The first assert passes while the
This is only tangentially a programming question. Where can I find a simple-to-parse list
Can any one explain why the output of this code is only 'hello' and
So the short version of this is: Can I traverse only the elements within
Is there anyway this code can be refactored? The only difference is the order
Can I copy only the error message from Eclipse Problems with instead of this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.