Thanks for your reply.
From reading around it sounds as though I am probably not meant to
solve this in the template (though this does seem like the easier
option).
I have figured out that I could solve this through an SQL statement as
follows:
select *, (select Count(*) FROM shop_track_owners where
track_id=
shop_track.id and user_id=<<<insert current user id
here>>>)"owned" from shop_track;
I have run this and it works. There are two problems though:
1: How do I do this through django querysets? I would like to keep the
database abstracted.
2: This will probably be rather awkward to do when talking about
artists or albums... Could I somehow modify the .track_set function of
the album model to not just return tracks (select * from shop_track)
but to run my other funky query?
Also I have had another thought: I could add an "owned" field to the
Track model. This could remain null for all tracks in the database
forever, but could be used in the view when grabbing the tracks to
pass the template whether or not each track is owned by the current
user. For example:
tracks = Track.objects.all()
for track in tracks:
if request.user in track.owners:
track.owned = True
Then render to response. Again, a couple of potential problems:
1: Does altering the track object (track.owned = True) also alter the
object within tracks? I don't know if it a reference or a copy. If it
is a copy, is there any way of shoving it back in the queryset?
2: There is still the problem of what to do when talking about albums.
Could you do something like:
for album in albums:
for track in album.track_set():
if request.user in track.owners:
track.owned = True
Again, would setting track.owned = True affect the track within the
album's track_set()? I would assume not... I think album.track_set()
executes a new database query to get the set, right? Is there any way
around this?
One last possibility that I can think of: is there any way of allowing
the track model to get at the request object? This would allow me to
write a method in the track model (owned) that checks if the track is
owned by the user in the request and then returns a bool.
Thanks again.
On Nov 12, 8:12 pm, Tomasz Zieliński