I have table cold Mark.
class Mark(models.Model):
media = models.ForeignKey('media.Media')
mark = models.PositiveIntegerField()
开发者_如何学Go user = models.ForeignKey(User)
class Meta:
unique_together = ('media_object','user')
How can i get query set of media instances (or just count the media) which has at least one vote?
Can i make it whith out using extra
?
UPDATED: I other words: I'm running through all table and counting all unique media. If i found it second time I'm not counting it. Other words: I need count unique media fields.
I'm assuming the "Mark" model is how users vote. To get all media models with their mark counts, you need aggregation:
from django.db.models import Count
media_with_vote_count = Media.objects.annotate(vote_count=Count('mark_set'))
You can then use filter()
that refers to that annotation:
voted_media = media_with_vote_count.filter(vote_count__gt=0)
There are also other useful aggregates. For example, you could calculate an average mark for every media:
from django.db.models import Avg
media_with_markavg = Media.objects.annotate(average_mark=Avg('mark_set__mark'))
mk = Mark.objects.all()
mk.media.count()
U can use the count function but not sure of ur question what u want to do from it or what is vote..
EDIT:
One row of media
if( mk.media.count() > 0):
......
精彩评论