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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:30:56+00:00 2026-06-18T04:30:56+00:00

I want to query a model, for single instance (eg., id=1 ), but I

  • 0

I want to query a model, for single instance (eg., id=1), but I only want to return the value of a single field ('title'). I can achieve this with the following query:

SomeModel.objects.filter(pk=1).values_list('title', flat=True)

But a get is more efficient than a filter. However, values_list and values don’t work with get queries. Ideally, I would prefer to do this:

SomeModel.objects.get(pk=1).values_list('title', flat=True)

But I get the following error: AttributeError: SomeModel has no attribute 'title'

What is the best way write this query?

  • 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-18T04:30:57+00:00Added an answer on June 18, 2026 at 4:30 am

    But a get is more efficient than a filter.

    This is simply not true. A .get(…) will generate exactly the same SQL as a .filter(…).

    Using:

    SomeModel.objects.filter(pk=1)[0:1].values_list('title', flat=True)
    

    Will do what you want, and will have exactly the same performance characteristics as .get(…) (actually, it will be a bit faster, because .get(…) checks to see if more than one row would be returned…):

    In [4]: import logging
    
    In [5]: l = logging.getLogger("django.db")
    
    In [6]: l.setLevel(logging.DEBUG)
    
    In [7]: User.objects.filter(id=1)[0:1].values_list()
    DEBUG:django.db.backends:(0.006) SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 1  LIMIT 1; args=(1,)
    Out[7]: [(1, u'admin', u'Admin', u'User', u'admin@example.com', u'sha1$bf3bc$daa1fb58a8a41e15c730ae86bc0faf4c01fdd3a1', True, True, True, datetime.datetime(2013, 1, 8, 21, 15, 51, 855527), datetime.datetime(2012, 1, 10, 15, 13, 55))]
    
    In [8]: User.objects.get(id=1)
    DEBUG:django.db.backends:(0.001) SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 1 ; args=(1,)
    Out[8]: <User: admin>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
I want to make the for loop to return a single value (TRUE/FALSE). But
This is my query to get model information from one table and a single
I am using the following models in my django application and want to query
I want a query which will return a combination of characters and number Example:
I want to query a table so that it´s ordered the following way: 1)
I want to query for an entity but exclude a bunch of keys/ids that
I want to get the value of a parameter in a sqlalchemy query object
I want to be able to group a sub-linq query, but LinqPad is giving
I have the following query that receives a list of IDs and I want

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.