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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:47:42+00:00 2026-06-16T21:47:42+00:00

Simple enough example – I have a bit of Django code that starts with

  • 0

Simple enough example – I have a bit of Django code that starts with a queryset…

queryset = MyModel.objects.all()

Later on it performs various filtering, depending on some configurable options…

if something:
    queryset = self.queryset.filter(foo=some_foo)

if another_thing:
    queryset = self.queryset.filter(bar=some_bar)

And finally it performs the lookup…

try:
    obj = queryset.get()
except ObjectDoesNotExist:
    raise ValidationError('Does not exist')

Now, because of the flexible way that the filtering needs to occur, it’s possible that the some_foo or some_bar variables might not be of the correct type (eg. we could end up with an empty string attempting to filter against an integer field.) so it’s possible for this code to end up raising a TypeError or a ValueError.

That’s fine, and I can handle the case appropriately, but what’s not clear to me from the ORM contract, is at what point should I expect those exceptions to be raised.

  • Will it occur on the .filter() statement?…
  • …or on the .get() statement?…
  • …or is in underspecified, and I handle it as able to occur on either? (Eg perhaps depending on the implementation of the database backend?)
  • 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-16T21:47:44+00:00Added an answer on June 16, 2026 at 9:47 pm

    To answer the original question, a FieldError and ValueError are raised on the call to filter, when a new queryset is built:

    >>> a = Account.objects.all()
    >>> a = a.filter(id=3)
    >>> a = a.filter(no_exist=3)
    <snip>
    FieldError: Cannot resolve keyword 'no_exist' into field. Choices are: active, created_on, group, id, ...
    
    >>> a = Account.objects.all()
    >>> a = a.filter(id='abc')
    ValueError: invalid literal for int() with base 10: 'abc'
    

    I’ll also add that this pattern seems misleading to me, in that filter is normally used to return a list/iterable of models, rather than one as with get. For clarity and easier handling of the exceptions, I’d suggest this pattern:

    kwargs = {}
    if something:
        kwargs['foo'] = some_foo
    if another_thing:
        kwargs['bar'] = some_bar
    
    # handle if kwargs is empty
    try:
        obj = MyModel.objects.get(**kwargs)
    except (FieldError, ValueError, ObjectDoesNotExist):
        raise ValidationError('Does not exist')
    

    The other added benefit is that, IIRC, the work of cloning querysets is relatively expensive, so you ignore that overhead, while at the same time making the code cleaner. Going back to your question, with this pattern there’s no question where the exception will be raised.

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

Sidebar

Related Questions

That seems simple enough, but all Django Queries seems to be 'SELECT *' How
Simple enough. If I have a container class that holds a Sprite object, and
Consider the following table with a constraint that's a bit daft but simple enough
Simple enough question I hope. I am using the following type of code to
Seems simple enough. I'm creating a search engine that returns results to the user
I have a list of products coming from a repository. Simple enough. Now I
I have an array of objects... for a concrete example, let's call the array
This should be simple enough, but somehow my brain stopped working. I have two
Simple enough but I can't find a decent example; so I asked here! Basically
I think the question is simple enough to understand.For more clarity I'm giving example

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.