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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:42:53+00:00 2026-05-23T12:42:53+00:00

How would I see if a value is in a QuerySet? For example, if

  • 0

How would I see if a value is in a QuerySet?

For example, if I have the following model:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    first_name = models.CharField(max_length=50)

How would I find out if the first_name ‘David’ is contained in a QuerySet? A way to do the following:

ld = UserProfile.objects.filter(...).values('first_name')
    >>> for object in ld:
    ...     if object['first_name'] =='David':
    ...             print True

Or if a particular user object is instead? Something like 'David' in QuerySet['first_name'] ? Thank you.

  • 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-23T12:42:54+00:00Added an answer on May 23, 2026 at 12:42 pm

    Simplest way is to use the get method of the manager:

    try:
        foo = Foo.objects.get(foo_name='David')
    except Foo.DoesNotExist:
        print 'Nope'
    except Foo.MultipleObjectsReturned:
        print 'Filter is a better choice here'
    

    The exists method is applicable also, if you don’t need to get the object:

    if Foo.objects.filter(foo_color='green').exists():
        print 'Nice'
    

    If you already have the object and want to determine if it is contained in a queryset:

    foo = Foo.objects.get(foo_name='David')
    qs = Foo.objects.filter(<criteria>)
    if foo in qs:
        print 'Nice again'
    

    If you want to use a value instead of an object:

    value = 'David'
    qs = Foo.objects.filter(<criteria>).values_list('foo_name',flat=True)
    if value in qs:
        print 'Nice'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to see if a value equals any of the values in
I have a list of checkboxes like you would see in most email clients
I would like to see how this example of existing code would be able
In this, I would see Readme, but the value would be set to 1
Not sure how I would check to see if the value of a nodes
I have a Django Form class defined likes this in Models : class AccountDetailsForm(forms.Form):
I would like to take my User model and break it down by day,
How can I create a Popup balloon like you would see from Windows Messenger
I've been messing around a bit with various solutions to what I would see
If you go to http://www.foxnews.com , and scroll down a little you would see

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.