Home > Mobile >  Django Queryset check value if exists
Django Queryset check value if exists

Time:11-09

I have two values to check if it's existed in my PostgreSQL. I also have columns named ref_name, ref_surname for example:

//this is the data//
name: John
Surname: Lee

//this is some queryset from django//
Employee.objects.filter(ref_name=name & ref_surname=Surname).exists()

I want to check if the data is already existed in my database. I have read the Queryset documentation and I can't find an answer. I'm open to any suggestion.

CodePudding user response:

Employee.objects.filter(ref_name=name, ref_surname=Surname).exists()

You don't have to put & which is an invalid syntax inside the filter() just separate them by a comma , it will give the same result.

CodePudding user response:

Change Employee.objects.filter(ref_name=name & ref_surname=Surname).exists()

to this Employee.objects.filter(ref_name=name, ref_surname=Surname).exists()

You can separate using comma not &

CodePudding user response:

if Employee.objects.filter(ref_name=name and ref_surname=Surname).exists()

  • Related