Re: (Noob) - filtering spanning relationship not working
Calling count() on a queryset will always aggregate the count of all rows. Please have a look at the annotate function in the documentation https://docs.djangoproject.com/en/dev/topics/db/aggregation/ Annotate will group your results and sum up the results.
I think what you'll have to do is:
assignment.finished_articles = Article.objects.filter(cowcode=country, pubdate__range(start_date, end_date), articlehistory__coder=request.user.id).annotate(number_of_articlehistory_entries=Count(articlehistory__id))
This should give you a list of Articles with an extra column 'number_of_articlehistory_entries' with the count of articlehistory entries per article. I'm pretty sure, that in this list all articles are shown, even those with 0 articlehistory entries.
If you only want those with more than 1 articlehistory entries, you should start the query from the articlehistory class.
assignment.finished_articles = ArticleHistory.objects.filter(article__cowcode=country, article__pubdate__range(start_date, end_date), coder=request.user.id).annotate(number_of_articlehistory_entries=Count(id))
A count on assignment.finished_articles should return the number of articles that have at least 1 connected articlehistory entry.
Hope this helps
Am Montag, 22. Juli 2013 09:51:14 UTC+2 schrieb Lukas Kawerau:
I want to find the number of
articles
for which a specific user has createdarticlehistory
records.
The models for that look like this:class Article(models.Model): """The basic entity of this app.)""" documentID = models.CharField(blank=True, max_length=1000) cowcode = models.IntegerField(blank=True
, null=True) pubdate = models.DateField(default=datetime .datetime.today) headline = models.CharField(blank=True, max_length=1500) source = models.CharField(blank=True, max_length=5000) text = models.TextField(blank=True, max_length=1000000) assignments = models.ManyToManyField(Assignment ) class Meta: ordering = ['pubdate'] def __unicode__(self): return self.headline class ArticleHistory(models.Model): """(Modelname description)""" article = models.ForeignKey(Article, related_name='Article History') coder = models.ForeignKey(User, related_name='Article History') last_updated = models.DateTimeField(default=datetime .datetime.now) def __unicode__(self): return self.last_updatedThe way I'm trying to do this at the moment is like this:
assignment.finished_articles = Article.objects.filter(cowcode
= country).filter(pubdate__range =(start_date,end_date), articlehistory__coder=request.user .id).count()This doesn't work, however and exhibits another weird behaviour:
I try to do this:for assignment in assignments: country = assignment.country.cowcode start_date = assignment.start_date end_date = assignment.end_date articles = Article.objects.filter(cowcode
= country).filter(pubdate__range =(start_date,end_date)).select_related () assignment.article_num = articles.count() #assignment.finished_articles = Article.objects.filter(cowcode=country).filter( pubdate__range=(start_date, end_date), articlehistory__coder=request. user.id ).count()This works fine, unless I try to include
finished_articles
, thenarticle_num
gets shortened to one result.It would be really great if anyone has a pointer to who to solve this.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home