tags:

views:

92

answers:

2

Hi!

I have following problem:

My application have 2 models:

1)

class ActiveList(models.Model):
    user = models.ForeignKey(User, unique=True)
    updatedOn = models.DateTimeField(auto_now=True)
    def __unicode__(self):
        return self.user.username

'''
GameClaim class, to store game requests.
'''
class GameClaim(models.Model):
    me = models.ForeignKey(ActiveList, related_name='gameclaim_me')
    opponent = models.ForeignKey(ActiveList, related_name='gameclaim_opponent')

In my view I took all ActiveList objects all = ActiveList.objects.all() and passed it to the template

In template I am looping through every item in the ActiveList, and create an xml file which is used on my client application.

the question is:

How can I query the info about the claims which one user (e.g. test, part of ActiveList), made to the user who is under loop

user2 e.g is taken like this

{%  for item in activeList  %}

{% endfor %}

user 2 is an item in this case

A: 

I'm not sure I entirely understand your question, but I think the information you're looking for might be here: http://docs.djangoproject.com/en/dev/topics/db/queries/

Perhaps you could clarify the question if you don't find an answer there?

John Debs
+1  A: 

What you are looking at doing belongs more properly in the view than the template. I think you want something like:

claimer = User.objects.get(name='test')
claimed_opponents = User.objects.filter(gameclaim_opponent__me__user=claimer)

Then you can pass those into your template, and operate on them directly.

You might also look at rethinking how your tables relate to one another. I think claims should probably go directly between users, and whether a given user is active should be external to the relationship. I would think a user should be able to claim a game with an inactive user, even if they have to wait for the user to reactivate before that game can begin.

jcdyer
Well, 1) I am trying to get a list of active players, it will be aggregated in the following way<players> <player> <name>Username</name> <was_called_by_me>1</was_called_by_me> <player><players> I found it easier to pass the entire list to the template, and then loop trrough it within the template. Not sure how should I pass every user there one by one... Any idea?2) I am thinking to add some interactivity to the game, so thinking to have claims only between active users.
Oleg Tarasenko