Hi all,

I am new to Django and I am having a little trouble.

The situation:
I am setting up a music selling website (similar to iTunes) for a uni
project. There are Tracks, Albums and Artists as well as users via
Django auth. A user can own a track. This is accomplished with a many
to many field in the Track model (owners = models.ManyToManyField
(User)).

The problem:
I want to put an "owned" tick next to each track that the user owns
throughout the site.

I cannot put a function in the Track class to return whether the
current user owns the track or not because track does not have access
to the user details (request.user).

I could return another data set with the same indices as the tracks
and have it store a boolean to indicate whether the current user owns
the track or not. Something along the lines of "owner[i] = tracks
[i].owners.filter(username__exact=request.user.username).count()".
Alternatively, do something like "Track.objects.extra(select={'owned':
"1"})" with some logic in there to figure out if it should be 1 or 0
(anyone know how to do something like that???). The trouble with
either of these methods, aside from not being very elegant, is that I
don't always just pass a tracks QuerySet to a template. I often pass
an albums QuerySet or an artists QuerySet, both of which are also used
to access tracks. For the case of a set of albums I would need to
iterate over all of them, iterate over all of their tracks and store
which tracks are owned in a 2D array. For the case of a set of artists
I would need to iterate over all of them, iterate over all their
albums, iterate over all of their tracks and store which ones are
owned in a 3D array. There has got to be a better way than this.

Is there a way of getting a model function to access user details so
that I can do a simple "if track.owned" in the template? Is there a
better solution to doing it in the view? Is there something I have not
thought of? What would be the "best practice" solution to this?

Thanks in advance for your help.

--

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


Reply via email to