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 in Python by kartik
• 20,470 points
222 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 by Niroj
• 43,540 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
190 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
• 57,550 points
13,310 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
126 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
3,609 views
+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,190 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 in Java by Niroj
• 43,540 points
146 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 in Python by Niroj
• 43,540 points
223 views
0 votes
1 answer

How to check if a value exists in pandas dataframe index?

Hello @kartik, Basically instead of raising exception I ...READ MORE

answered Jun 15 in Python by Niroj
• 43,540 points
312 views