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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:27:46+00:00 2026-05-22T22:27:46+00:00

In Django, queryset provides a method called get_or_create that either returns an objects or

  • 0

In Django, queryset provides a method called get_or_create that either returns an objects or creates an object.

However, like the get method, get_or_create can throw an exception if the query returns multiple objects.

Is there an method to do this elegantly:

objects = Model.manager.filter(params)
if len(objects) == 0:
   obj = Model.objects.create(params)
else:
   obj = objects[0]
  • 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-22T22:27:47+00:00Added an answer on May 22, 2026 at 10:27 pm

    get_or_create() is just a convenience function so there’s nothing wrong with writing your own, like pavid has shown or

    result = Model.objects.filter(field__lookup=value)[0]
    if not result:
       result = Model.objects.create(...)
    return result
    

    EDIT
    As suggested, changed the [:1] slice (which returns a single-entry list) after the filter to [0] (which returns the actual object). The problem with this is it will raise an exception if there is not match to the query.

    This will also raise a simliar exception:

    Model.objects.filter(field_lookup=value).latest()
    

    Looking at the question again, I’m not sure whether the original poster is looking to return multiple objects/rows, or just a way to get around raising an exception when retrieving a single object/row.

    Here’s another option?

    results = Model.objects.filter(...)
    if results.exists():
        return results
    else:
        return Model.objects.create(...)
    

    and another:

    result = None
    try:
        result = Model.objects.get(...)
    except Model.DoesNotExist:
        result = Model.objects.create(...)
    

    There’s nothing wrong with raising & catching exceptions!

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

Sidebar

Related Questions

I want to combine an object and a QuerySet in Django. My objects are
I have a list of objects from a django queryset, e.g. my_list = MyObject.objects.filter(variable=something)
I know that you can get the SQL of a given QuerySet using print
How do I get the SQL that Django will use on the database from
I'd like to filter a Django queryset using an array as a constraint on
I would like to filter a queryset by whether a certain subquery returns any
Customizing a queryset of a form field in Django isn't a hard job. Like
I have a Django Model that implements a time range, like this: class Period(models.Model):
I have such QuerySet in Django. j = piosenki.objects.select_related('projekt__postac').distinct().exclude(typ__in=[live, remix]).filter(piosenki__plyta__status=1, piosenki__plyta__typ='s') .extra(select={ 'ocena' :
Is it possible to filter a Django queryset by model property? i have a

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.