Skip to content

Conversation

@bkvirendra
Copy link

This method will return a boolean value, whether the result for the query exists or not.

This method will return a boolean value, whether the result for the query exists or not.
@rozza
Copy link
Contributor

rozza commented Jun 24, 2013

Hi thanks for the pull request, it can't be merged currently as it needs tests.

@bkvirendra
Copy link
Author

I ll add the tests, and send update the pull request! Does that work? @rozza

@MRigal
Copy link
Member

MRigal commented Dec 4, 2013

exists() could be a nice shortcut, but doing with len() is actually the wrong way of doing it. It is much slower than testing the existence of a result when limiting the queryset to one. And exists() should be faster than len(), like for other DB engines, else it is useless

@jkarni
Copy link

jkarni commented Apr 8, 2014

And anyhow, in the absence of __nonzero__ the truth-value of the object itself already is len(obj) == 0.
Like MRigal, I also think this could be a useful feature if implemented in a more efficient way. I wouldn't mind submitting a PR for it - would it be welcome?

@rochacbruno
Copy link
Contributor

👍

2 similar comments
@emanuelvianna
Copy link

👍

@ispmarin
Copy link

👍

@rochacbruno
Copy link
Contributor

I also think this could be a useful feature if implemented in a more efficient way. I wouldn't mind submitting a PR for it - would it be welcome?

@jkarni It will be very welcome, can you do it or maybe give some hint?

@jonathansp
Copy link

Fix with tests #655
Implements __nonzero__(),__bool__() and has_data()
It does not use __len__ because it will open all documents. If queryset points to 100 documents, 100 queries wil be done. This fix checks only 1 record (1 query) in queryset to return True or False

Use of:

if Person.objects.has_data(): 
    # do something...

is equivalent to

if Person.objects: 
    # do something...

What do you think? @wojcikstefan @rozza ?
Thank you for the tips @wojcikstefan!

@wojcikstefan
Copy link
Member

I'd close this PR and handle it in #655 - most of the work is done there already. Anyway, I think @rozza is not a maintainer of this repo anymore, @lig is (right?).

@rozza
Copy link
Contributor

rozza commented Jun 27, 2014

#665 has been merged covering this

@rozza rozza closed this Jun 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants