How to check if an element is present in a Django queryset

0 votes

Is it like a regular python set?

Suppose I have the following queryset

entry_set = Entry.objects.all()

How do I check if Entry Object e is present in entry_set?

May 27, 2020 in Python by kartik
• 37,490 points
4,868 views

1 answer to this question.

0 votes

Hello @kartik,

You can use the following code:

if e in Entry.objects.all():
        #do something

Or the best approach:

if Entry.objects.filter(id=e.id).exists():

Hope this works!!

answered May 27, 2020 by Niroj
• 82,680 points

Related Questions In Python

0 votes
1 answer

How to check if a substring is present in a string using Python?

To check if the substring exists in ...READ MORE

answered May 9, 2019 in Python by Sharan
325 views
+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 58,100 points
19,236 views
0 votes
1 answer

How to check if a list is empty in python?

To check if a list is empty ...READ MORE

answered May 27, 2019 in Python by Nisa
• 1,090 points
216 views
0 votes
2 answers

How to check if a csv file is empty in pandas?

Try this: df = pd.DataFrame(columns=['Name', 'ID', 'Department']) if df.empty ...READ MORE

answered Jul 1, 2019 in Python by Bob
6,778 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,981 views
0 votes
0 answers
0 votes
1 answer

What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

Hii @kartik, As always with Android there's lots ...READ MORE

answered May 26, 2020 in Java by Niroj
• 82,680 points
1,181 views
0 votes
1 answer

How to check if any value is NaN in a Pandas DataFrame?

Hello @kartik, If you need to know how ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,680 points
2,117 views
0 votes
1 answer

How to check if a float value(decimal number) is between range(0 to 10) in python

Hello,  A function that returns True for an integer number (int or ...READ MORE

answered Sep 13, 2020 in Python by Niroj
• 82,680 points
2,672 views