> I'm not sure i understand you question.  You can filter via the  
> attributes of a related object like this:
> Recording.objects.filter(song__artist="Ben Folds")

Yes, and that's perfectly all right when you are only interested only  
in the related objects.
But what do you do when you want to do something like:

"Give my all songs that start with the letter R AND limit the related  
recording_set to the recordings that have represents_song=True"?

You could do this with two queries:
songs = Song.objects.filter(title__startswith='R')
recordings = Recording.objects.filter(represents_song=True)

But then there would be no connection between songs and recordings.
Every song in songs would still have attached all recordings that  
belong to the song, not only the recordings with represents_song=True.

What I want is to filter objects and at the same time the FOO_sets of  
the single objects.
When I understand this right, with filters you can either filter the  
objects itself OR you can filter the related objects, but not both at  
the same time.

I think this can only be don via a custom manager or a custom method.

As I don't understand how custom managers work yet to well, I tried it  
with a custom method:

class Song(models.Model):
     title                   = models.CharField(max_length=50)
     description             = models.TextField(blank=True)

     def __unicode__(self):
         return self.title

     def get_representative_recording(self):
         representative_recording =  
self.recording_set.get(represents_song=True)
         return representative_recording

class Recording(models.Model):
     song                    = models.ForeignKey(Song)
     mp3                     = models.FileField(upload_to='songs')
     represents_song         = models.BooleanField()

I can now filter the songs in the view:

     songs = Song.objects.filter(filter_for_whatever_you_like_here)

And access the "representative recording" like this in the template:

     {{ song.get_representative_recording.mp3.url }}

I can't see how this could be done only by filtering, without a custom  
method or a custom manager.

-benjamin



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to