Re: How to filter FOO_set in templates?

2008-08-23 Thread Garrett Garcia
On Sat, Aug 23, 2008 at 11:50 AM, Benjamin Buch <[EMAIL PROTECTED]> wrote:

>
> Am 22.08.2008 um 23:15 schrieb Garrett Garcia:
>
> How about:
>
> songs = Recording.objects.filter(song__title__startswith='R',
> represents_song=True)
>
> http://www.djangoproject.com/documentation/db-api/#filter-kwargs
>
> "Multiple parameters are joined via AND in the underlying SQL statement."
>
> - Garrett
>
>
> The problem I still see with this is that this is a query set containing
> recordings, not songs.
> Let's assume there can be several recordings that represent the song and
> thus can have "represents_song=True".
> If you had the query from above,
>
> songs = Recording.objects.filter(song__title__startswith='R',
> represents_song=True),
>
> how would you display the songs in the template in a way that the songs and
> recordings would order like this:
>
> song_1_title
>   song_1_representative_recording_1
>   song_1_representative_recording_2
>   song_1_representative_recording_3
>
> song_2_title
>   song_2_representative_recording_1
>   song_2_representative_recording_2
>
> ...
>
> ?
>
> -benjamin
>


You're right.  Using the word "recordings" as the variable is less
confusing.  I was only using "songs" because that's what you called it in
your original question.

 recordings = Recording.objects.filter(song__title__startswith='R',
represents_song=True).order_by(song__title)

You can then loop through the list of recordings in the template.  Use the
ifchanged tag to only print the song title once for each song.  Also, it
seems to me that you WANT to return a list of recordings, not songs, since
you'll be accessing the mp3 object of each recording.

Filtering this way is not a workaround, it's the simplest and easiest way to
do it.



> >
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to filter FOO_set in templates?

2008-08-23 Thread Benjamin Buch

Am 22.08.2008 um 23:15 schrieb Garrett Garcia:

> How about:
>
> songs = Recording.objects.filter(song__title__startswith='R',  
> represents_song=True)
>
> http://www.djangoproject.com/documentation/db-api/#filter-kwargs
>
> "Multiple parameters are joined via AND in the underlying SQL  
> statement."
>
> - Garrett

The problem I still see with this is that this is a query set  
containing recordings, not songs.
Let's assume there can be several recordings that represent the song  
and thus can have "represents_song=True".
If you had the query from above,

songs = Recording.objects.filter(song__title__startswith='R',  
represents_song=True),

how would you display the songs in the template in a way that the  
songs and recordings would order like this:

song_1_title
   song_1_representative_recording_1
   song_1_representative_recording_2
   song_1_representative_recording_3

song_2_title
   song_2_representative_recording_1
   song_2_representative_recording_2

...

?

-benjamin
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to filter FOO_set in templates?

2008-08-23 Thread Benjamin Buch

Am 22.08.2008 um 23:15 schrieb Garrett Garcia:
> How about:
>
> songs = Recording.objects.filter(song__title__startswith='R',  
> represents_song=True)
>
> http://www.djangoproject.com/documentation/db-api/#filter-kwargs
>
> "Multiple parameters are joined via AND in the underlying SQL  
> statement."
>
> - Garrett

Hm, never thought about that. Thanks!

But still: It feels somehow like a workaround. I want to have songs in  
my query set, not recordings.
songs = Recording looks confusing to me. Songs are songs, and  
recordings are recordings.

And I still would have to acess the song via the recording in the  
template:
{{ recording.song.title }}

-benjamin
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to filter FOO_set in templates?

2008-08-22 Thread Garrett Garcia
On Fri, Aug 22, 2008 at 3:24 AM, Benjamin Buch <[EMAIL PROTECTED]> wrote:

>
>
>
> > 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
>

How about:

songs = Recording.objects.filter(song__title__startswith='R',
represents_song=True)

http://www.djangoproject.com/documentation/db-api/#filter-kwargs

"Multiple parameters are joined via AND in the underlying SQL statement."

- Garrett

>
>
>
> >
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to filter FOO_set in templates?

2008-08-22 Thread Benjamin Buch



> 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 django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: How to filter FOO_set in templates?

2008-08-21 Thread Garrett Garcia
On Wed, Aug 20, 2008 at 11:17 PM, Benjamin Buch <[EMAIL PROTECTED]> wrote:

>
>
> Hi Garret,
>
> > How about sending the template something like:
> >
> > songs = Recording.objects.filter(represents_song=True)
> >
> > A custom manager seems like overkill for this situation (If I'm
> > understanding you right).
> >
> > - Garrett
>
> this is indeed a plausible sollution.
> I then had to access the songs' data via the recording in the template
> like
>
> {% for recording in songs %}
>{{ recording.song.title }}
>{{ recording.song.description }}
>{{ recording.mp3 }}
> {% endfor %}
>
> For my special case I think this should work, because every song has
> exact one recording that represents it.
> And you are right, a custom manager seems like overkill in this case.
> But out of interest, it turned out to be a more general question for me:
>
> How to limit the query set of a related object?
>
> I think this can only be done via a custom manager...
>
> Thanks!
>
> -benjamin
>

Benjamin,

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")

For more on the filter tag, go here:
http://www.djangoproject.com/documentation/db-api/#filter-kwargs

As I understand it, a custom manager is just a convenience to abstract out
commonly used filter patterns.  This means that there is nothing that "can
only be done via a custom manager" that can't also be done directly to a
queryset.  (Please someone correct me if I'm wrong.)

For more on custom managers, read this:
http://www.djangoproject.com/documentation/models/custom_managers/

- Garrett

>
> >
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to filter FOO_set in templates?

2008-08-21 Thread Benjamin Buch


Hi Garret,

> How about sending the template something like:
>
> songs = Recording.objects.filter(represents_song=True)
>
> A custom manager seems like overkill for this situation (If I'm  
> understanding you right).
>
> - Garrett

this is indeed a plausible sollution.
I then had to access the songs' data via the recording in the template  
like

{% for recording in songs %}
{{ recording.song.title }}
{{ recording.song.description }}
{{ recording.mp3 }}
{% endfor %}

For my special case I think this should work, because every song has  
exact one recording that represents it.
And you are right, a custom manager seems like overkill in this case.
But out of interest, it turned out to be a more general question for me:

How to limit the query set of a related object?

I think this can only be done via a custom manager...

Thanks!

-benjamin

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to filter FOO_set in templates?

2008-08-19 Thread Garrett Garcia
How about sending the template something like:

songs = Recording.objects.filter(represents_song=True)

A custom manager seems like overkill for this situation (If I'm
understanding you right).

- Garrett


On Sat, Aug 16, 2008 at 2:34 PM, Benjamin Buch <[EMAIL PROTECTED]> wrote:

>
> I have a many-to-one-relationship with recordings and songs, so that
> one song can have several recordings.
> In a view, I filter the songs by certain parameters and pass a
> variable 'songs' to the template.
>
> In the template, I just want to display the recordings which have the
> attribute 'represents_song', which is boolean, set to true.
> This is always just one recording.


> As I can't filter the recordings in the view, I have to filter them in
> the template.
> Is there any possibiltiy to filter {{ for recording in
> song.recording_set.all }}?
> And by the way: Why has it to be FOO_set.all and not FOO_set?
>
> -benjamin
>
> >
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---