Monday, July 22, 2013

(Noob) - filtering spanning relationship not working

I want to find the number of articles for which a specific user has created articlehistoryrecords.
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_updated

The 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, then article_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.
 
 

No comments:

Post a Comment