Home > Software design >  Return value if not exist
Return value if not exist

Time:12-09

Within my template, I have a number of values being presented from data within the database.

{{ fundamentals.project_category }}

But when no data exists it throws an error matching query does not exist. i think because the no data is being returned in the query set within the fundamentals model. fundamentals = project.fundamentals_set.get()

within my view im trying:

    if project.fundamentals_set.get().exists():
        fundamentals = project.fundamentals_set.get()
    else:
        #what should i put here? 

Im assuming an if statment is requried along with exists(): but this isn't working and im not sure what i should put in the else statement to return something like nothing exists when no data exists within the fields?

CodePudding user response:

Call exists() on a queryset. Your call to get() in your condition is executing a query which you don't want if there could be no results.

if project.fundamentals_set.all().exists():
    fundamentals = project.fundamentals_set.get()
else:
    # what should i put here? 
  • Related