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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:35:32+00:00 2026-06-05T16:35:32+00:00

Using django, which is the way to check if data is present? I know

  • 0

Using django, which is the “way” to check if data is present?

I know i can either have a try/catch block on a get, or check the size of a len on a filter

ie

try:
    DemoModel.objects.get(id=8)
catch DoesNotExist:
    catch stuff here

or

if not len(DemoModel.objects.filter(id=8):
    do stuff here

i suppose i’m defining “best” as

a. the standard way
b. the more efficient way

or is there no real difference? Or is there a non-partisan third way?

  • 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-05T16:35:34+00:00Added an answer on June 5, 2026 at 4:35 pm

    If you need to use the object, use try/catch.

    try:
        object = DemoModel.objects.get(id=8)
        #use object here
    catch DoesNotExist:
        #catch stuff here
    

    If you don’t need to, just use exists().

    if DemoModel.objects.filter(id=8).exists():
        #do stuff here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had the following idea: Say we have a webapp written using django which
I'm using django, and have a static webpage with a GET form, and about
I am using django Generic views which straigt way loads the create form. I
I'm using django-registration which somehow stopped working the way it should work. I'm completely
I'm using django-transmeta (couldn't get anything else working better with django 1.2.5) which creates
I have a simple mail sending application which runs in python using Django. After
I'm displaying user messages through templates using RequestContext in Django, which gives access to
using django 1.4 I have a model with a datetimefield. I imported django.utils.timezone to
I just started using django for development. At the moment, I have the following
I am using Django's comment framework which utilizes generic foreign keys. Question: How do

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.