3
You can count the number of related objects by annotating:
from django.db.models import Count
MyText.objects.filter(
nauthors=Count('authors')
).filter(nauthors__gte=1)
Here you are however filtering on MyText
s with at least one author. You can do that by filtering for non-NULL
values and then return a distinct set:
MyText.objects.filter(authors__isnull=False).distinct()
3
The proper way to do this is shown here.
What you need to is that first annotate each TweetJson
with the count of authors
:
from django.db.models import Count
TweetJson.objects.annotate(num_authors=Count('authors'))
And, after this you can perform the __gte
lookup on annotated field:
TweetJson.objects.annotate(num_authors=Count('authors')).filter(num_authors__gte=1)
- [Django]-Django Markdown Editor does not show up
- [Django]-Django: Timezone stored in MySQL DB is not correct
- [Django]-Django. Get only current day. Only day without time
- [Django]-Websocket connection not working in Django Channels ('WebSocket connection to 'ws://localhost:8000/ws/board/7/' failed:')
- [Django]-Select default value of <select> element in Django
2
you can count like this also:
from django.db.models import Count
MyText.objects.annotate(no_of_authors=Count('authors')).filter(no_of_authors__gte=1)
Hope this will work for you.
- [Django]-Adding new form fields dynamically in admin
- [Django]-Implementing Name Synchronization and Money Transfers in Transactions Model with Account Number Input
- [Django]-Best Practice for following Subversion repository Trunk: Git (Mirror), Git-Svn, Subversion?
- [Django]-Filtering by calculated (extra()) fields
- [Django]-Problem rendering a Django form field with different representation
Source:stackexchange.com