Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django check for any exists for a query

In django how to check whether any entry exists for a query

sc=scorm.objects.filter(Header__id=qp.id) 

This was how it was done in php

if(mysql_num_rows($resultn)) {     // True condition     } else {     // False condition     } 
like image 229
Hulk Avatar asked Apr 22 '10 11:04

Hulk


People also ask

How do you check is exists in Django?

You can use exists() : if scorm. objects. filter(Header__id=qp.id).

Does exist in Django?

exists() is useful for searches relating to both object membership in a QuerySet and to the existence of any objects in a QuerySet, particularly in the context of a large QuerySet.

What is F in Django QuerySet?

In the Django QuerySet API, F() expressions are used to refer to model field values directly in the database.


1 Answers

As of Django 1.2, you can use exists():

https://docs.djangoproject.com/en/dev/ref/models/querysets/#exists

if some_queryset.filter(pk=entity_id).exists():     print("Entry contained in queryset") 
like image 79
sdornan Avatar answered Oct 14 '22 21:10

sdornan