On Wed, Jun 10, 2009 at 9:35 PM, ssc<steven.samuel.c...@gmail.com> wrote: > > [...] > > class Story(models.Model): > # fields like headline, content, etc. not shown here > # Google groups seems to word-wrap at 75 characters... > classifications = models.ManyToManyField(Classification, \ > > through="StoryPublishDate") > > class StoryPublishDate(models.Model): > # some stuff omitted here, too > story = models.ForeignKey(Story, related_name='publish_dates') > classification = models.ForeignKey(Classification, \ > > related_name='story_publication') > publish_date = models.DateTimeField() > > class Classification(our.own.TreeModel): > # ... > name = models.CharField(max_length=255) > slug = models.SlugField(unique=True) > > [...] > > I am struggling to get the story filtering right. My problem is that > there is more than one publish dates and possibly more than one > classification: > > # for demo purposes, get the Hot Gossip classification by its slug; > # comes in as parameter in the real system, of course... > classification = Classification.get(slug='hot-gossip') > [...] > stories = Story.objects.filter(classifications = classification)
Shouldn't this be stories = classification.story_set.all() instead?. Then you can filter that queryset it further ad you've done below. > > # filtering by date range then seems pretty straight forward, e.g.: > today = date.today() > start_date = today - timedelta(days = 30) > end_date = today - timedelta(days = 7) > stories_month = \ > stories.filter(publish_dates__publish_date__range=(start_date, > end_date)) > > Of course, I thought of the 'native' approach to iterate over > story.classifications.all() or story.publish_dates.all() in the view > Python code and check if the classification and date match, but then I > seem to be working with Python lists instead of Django QuerySets. This > might be the way to go, using e.g. > http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view, > but I think this is a rather common problem, so maybe someone knows > how to solve this more elegantly. > > Can anyone help me ? > > Cheers, > > Steve > -- Ramiro Morales http://rmorales.net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---