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

The Archive Base Latest Questions

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

employee = Employee.objects.filter(‘age’ = 99) We assume this queryset is empty. If I use

  • 0
employee = Employee.objects.filter('age' = 99)

We assume this queryset is empty.

If I use employee[0], that will return index out of range error, so is it possible to have a None as default value here?

`employee[0] or None`? # This won't work, but this is what I mean.
  • 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-19T12:23:00+00:00Added an answer on May 19, 2026 at 12:23 pm

    If you just want to get a single instance, use get, not filter:

    employee = Employee.objects.get(age = 99)
    

    If that doesn’t exist, you’ll get a Employee.DoesNotExist exception, which you’ll need to catch. If there’s more than one 99 year-old employee, you’ll get a Employee.MultipleObjectsReturned exception, which you may want to catch.

    There’s always django-annoying‘s get_object_or_None if you’re feeling lazy!

    from annoying.functions import get_object_or_None
    
    obj = get_object_or_None(Employee, age=99)
    

    If you don’t want to use all of django-annoying, you can always add get_object_or_None somewhere, it just looks like this:

    def get_object_or_None(klass, *args, **kwargs):
        """
        Uses get() to return an object or None if the object does not exist.
    
        klass may be a Model, Manager, or QuerySet object. All other passed
        arguments and keyword arguments are used in the get() query.
    
        Note: Like with get(), a MultipleObjectsReturned will be raised if
        more than one object is found.
        """
        queryset = _get_queryset(klass)
        try:
            return queryset.get(*args, **kwargs)
        except queryset.model.DoesNotExist:
            return None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find/create an application that will create employee web usage reports from
I've got a object (Filter) that's defined something like this public class Filter {
Dealing with an employee that went over my head
I've found an article on this subject by a Microsoft employee, but has anyone
I'm writing an app that contains the following tables: (1) employee_type, (2) employee and
I have a model class Employee_Type(models.Model): def __unicode__(self): return self.name name = models.CharField(max_length=200, verbose_name=employee
I have an ObservableCollection of about 1000 objects that needs to be filtered (searched)
I have this situation where I want to display a list of Administration objects
If you create a Filter object that contains criteria for Linq that normally goes
We had an employee leave the company, and they left some files checked out

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.