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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:44:16+00:00 2026-06-14T06:44:16+00:00

Lets say I want to get a record that match a value and I

  • 0

Lets say I want to get a record that match a value and I have 2 approaches to do it:

First:

try:
  obj = Model.objects.get(field = value)
except
  pass

Second:

if Model.objects.filter(field = value).count() > 0:
  obj = Model.objects.filter(field_value)[0]

Lets put the code comments aside,which way should I use or which one do you prefer to read? The first one seems faster because only 1 DB look up, but the second way seems a bit more readable, but requires 2 DB look ups.

  • 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-14T06:44:17+00:00Added an answer on June 14, 2026 at 6:44 am

    The first is preferred in Python, based on the EAFP design principle (“Easier to Ask Forgiveness than Permission”). Aside from speed, one advantage of this system is that there is no race condition — in the second example, if other concurrent access to the database changes the results between the execution of the first and the second line of code, then your results will be inconsistent.

    Depending on your use of transactions, the race condition possibility may be a non-issue, but in general EAFP is a prominent design pattern in Python and experienced Python coders will not have any trouble reading code in that form.

    ETA: Oh, and I forgot: don’t use except: (you do need the colon). Use except IndexError: or whatever other specific exception you are looking for. That way if you get a totally unexpected error like a failure to reach the database, it will propagate through and not be hidden. You don’t want a situation where you later write code counting on the exception being thrown to mean “there are no results” and instead the system is trying to tell you “the database is down”.

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

Sidebar

Related Questions

Lets say I have a selector $('.items') and I want to get every item
Let’s say that my string is: test! I want to get the first character
Let's say I want to get a have a database that contains days of
lets say you want to get all the columns of a table, but exclude
I want to get coverage of let's say CoverageTarget.dll. I have a test.dll to
I have a model for which I want to retrieve the next record(s) and
Let's say that I want to get the size in bytes or in chars
I have a DateTime column. I want to extract all records, lets say, from
Lets say that I have this records in a dataset: name1 XXXXX XXXXXX name1
My simplified and contrived example is the following:- Lets say that I want to

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.