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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:33:13+00:00 2026-06-10T08:33:13+00:00

I am implementing a one off data importer where I need to search for

  • 0

I am implementing a one off data importer where I need to search for existing slugs. The slugs are in an array. What is the accepted best practices way of converting an array to an OR query?

I came up with the following, which works, but feels like way too much code to accomplish something this simple.

# slug might be an array or just a string
# ex:
slug = [ "snakes", "snake-s" ] # in the real world this is generated from directory structure on disk

# build the query
query = MyModel.objects
if hasattr(slug, "__iter__"):
    q = Q()
    for s in slug:
        q = q.__or__(Q(slug=s))
    query = query.filter(q)
else:
    query = query.filter(slug=slug)
  • 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-06-10T08:33:15+00:00Added an answer on June 10, 2026 at 8:33 am
    slug = ["snakes", "snake-s" ] # in the real world this is generated from directory structure on disk
    
    # build the query
    query = MyModel.objects
    if hasattr(slug, "__iter__"):
        q_list = []
        for s in slug:
            q_list.append(Q(slug=s))
        query = query.filter(reduce(operator.or_, q_list))
    else:
        query = query.filter(slug=slug)
    
    • q_list = [] create a list of Q clauses
    • reduce(operator.or_, q_list) implode the list with or operators

    read this: http://www.michelepasin.org/techblog/2010/07/20/the-power-of-djangos-q-objects/

    @MostafaR – sure we could crush my entire codeblock down to one line if we wanted (see below). Its not very readable anymore at that level though. saying code isn’t “Pythonic” just because it hadn’t become reduced and obsfucated is silly. Readable code is king IMHO. Its also important to keep in mind the purpose of my answer was to show the reduce by an operator technique. The rest of my answer was fluff to show that technique in context to the original question.

    result = MyModel.objects.filter(reduce(operator.or_, [Q(slug=s) for s in slug])) if hasattr(slug, "__iter__") else MyModel.objects.filter(slug=slug)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on implementing a fulltext search engine for one of our sites,
This question is somewhat similar to this one Best way to deploy large *.war
i am implementing one app related to animation. My require ment is, i have
i am implementing one graph related app. In that when application start i am
I have two Java interfaces and one implementing class. (I have used Eclipse to
I might be missing something obvious here, but I'm implementing NSCopying on one of
which one is better for implementing chat for the iPhone with users in the
I am implementing a image rotator on one of my pages, however I would
When implementing a hash table using a good hash function (one where the probability
When implementing the Strategy Pattern, where does one put the code that determines which

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.