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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:29:04+00:00 2026-06-12T11:29:04+00:00

There is probably a better way of dealing with non existant query sets…! The

  • 0

There is probably a better way of dealing with non existant query sets…!
The problem i have with this code is that it raises an exception if the normal case will be true! That is: if a workspace name with the same name in the db is not existent.

But instead of having an exception i would like to go for a query that does not return DoesNotExist but true or false

My unelegant code:

 try:
            is_workspace_name = Workspace.objects.get(workspace_name=workspace_name,user=self.user.id )
 except:
        return workspace_name
 if is_workspace_name:
            raise forms.ValidationError(u'%s already exists as a workspace name! Please choose a different one!' %workspace_name  ) 

Thanks a lot!

  • 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-12T11:29:06+00:00Added an answer on June 12, 2026 at 11:29 am

    You can use exists() method. Quoting docs:

    Returns True if the QuerySet contains any results, and False if not.
    This tries to perform the query in the simplest and fastest way
    possible, but it does execute nearly the same query as a normal
    QuerySet query.

    Remarks: the simplest and fastest way. It is cheaper to use exists (than count) because with exists the database stops counting at first occurrence.

    if Workspace.objects.filter(workspace_name=workspace_name, 
                                user=self.user.id).exists()
        raise forms.ValidationError(u'%s already exists ...!' % workspace_name)
    else:
        return workspace_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Javascript below, and I know that there is probably a better
I know there is probably a better way of doing this, I have tried
There's probably a better way to do this than what I'd doing, because I'm
There's probably a better way to do this, but this is what I'm trying
There is probably a better way to accomplish this. I click on this button
Ok, there is probably a better way of doing this, and if this is
I've created a salt using; md5(rand(0,10000000)); (there is probably a better way?) There doesn't
There's probably a silly error in my code. I have defined the following variables:
I have this script which calculates days until someone's next birthday (there's probably a
I'm a total PHP newb, so there is probably a better way of outputting

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.