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 3276942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:18:55+00:00 2026-05-17T19:18:55+00:00

I have a Django form setup using GET method. Each value corresponds to attributes

  • 0

I have a Django form setup using GET method. Each value corresponds to attributes of a Django model. What would be the most elegant way to generate the query? Currently this is what I do in the view:

def search_items(request):
    if 'search_name' in request.GET:
        query_attributes = {}

        query_attributes['color'] = request.GET.get('color', '')
        if not query_attributes['color']: del query_attributes['color']

        query_attributes['shape'] = request.GET.get('shape', '')
        if not query_attributes['shape']: del query_attributes['shape']

        items = Items.objects.filter(**query_attributes)

But I’m pretty sure there’s a better way to go about it.

  • 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-17T19:18:55+00:00Added an answer on May 17, 2026 at 7:18 pm

    You could do it with a list comp and and “interested params” set:

    def search_items(request):
        if 'search_name' in request.GET:
            interested_params = ('color', 'shape')
            query_attrs = dict([(param, val) for param, val in request.GET.iteritems() 
                                if param in interested_params and val])
    
            items = Items.objects.filter(**query_attrs)
    

    Just for fun (aka don’t actually do this) you could do it in one line:

    def search_items(request):
        items = Items.objects.filter(
            **dict([(param, val) for param, val in request.GET.iteritems() 
                    if param in ('color', 'shape') and val])
        ) if 'search_name' in request.GET else None 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a Django form such as: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message
I have a web report that uses a Django form (new forms) for fields
I have a Django model with a large number of fields and 20000+ table
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have a blog written in Django, and I started using the basic Django
Has anyone setup django-paypal? Here is the link to it here ? I have
The setup = I have this class, Transcript: class Transcript(models.Model): body = models.TextField('Body') doPagination
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker
Does Django have any template tags to generate common HTML markup? For example, I

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.