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 }
You can use exists() : if scorm. objects. filter(Header__id=qp.id).
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.
In the Django QuerySet API, F() expressions are used to refer to model field values directly in the database.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With