Re: Limit choices based on a foreign key

2009-10-12 Thread Christian Wittwer

Ok, I found a way, but it's still not perfect.

class GoalForm(forms.ModelForm):

class Meta:
model = Goal

def __init__(self, *args, **kwargs):
super(GoalForm, self).__init__(*args, **kwargs)
self.fields['goal_scorer'].queryset =
Player.objects.filter(gameroster__game=self.instance.game)

class GoalInline(admin.TabularInline):
model = Goal
extra = 4
#form = GoalForm


class GameAdmin(admin.ModelAdmin):
list_display = ('date_time', 'home_team', 'opponent_team',
'is_home_game', 'season', 'league', 'type', 'result')
list_filter = ['league', 'season']
inlines = [GameRosterInline, GoalInline, PenaltyInline]
ordering   = ('date_time',)

That works as long as I edit the model game not inline. The selection
of player gets limited as I want.
When I try to edit it inline in the game, the GoalForm seems not to
work. It's going to be ignored.
When I try to comment in "form = GoalForm", the whole app doesn't run
anymore. I got a DoesNotExist exception, but I think this is because
it can't register the form..

Any ideas who to activate my custom form for the inline mode?

Chris


2009/10/12 Christian Wittwer <wittwe...@gmail.com>:
> Hi,
> I have a question regarding the admin interface of django.
>
> My models
>
> class Team(models.Model):
>    name = models.CharField(max_length=10)
>
> class Player(models.Model):
>    lastname = models.CharField(max_length=60)
>
> class Goal(models.Model):
>    game = models.ForeignKey(Game)
>    goal_scorer =
> models.ForeignKey(Player,related_name='goal_scorer',blank=True,null=True)
>
> class Game(models.Model):
>    home_team = models.ForeignKey(Team,related_name='home_team')
>
> class GameRoster(models.Model):
>    player = models.ForeignKey(Player)
>    game = models.ForeignKey(Game)
>
> Each Goal is scored by a Player in a Game. I'm using a inline form to
> insert goals for a game.
> The players are somehow connected to the team (gameroster), and
> therefore to the game.
>
> I found the place to hook in to limit the choices of player.
>
> class GoalInline(admin.TabularInline):
>    model = Goal
>    extra = 4
>
>    def formfield_for_foreignkey(self, db_field, request, **kwargs):
>        if db_field.name == "goal_scorer":
>            kwargs["queryset"] =
> Player.objects.filter(gameroster__game=self.game)
>            return db_field.formfield(**kwargs)
>
>        return super(GoalInline,
> self).formfield_for_foreignkey(db_field, request, **kwargs)
>
>
> The missing point is now: How to access game, which is connected to
> the goal to limit the players?
> I tried self.game as you can see, but that doesn't work.
> How can I access the model from within this function?
>
> Chris
>

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Limit choices based on a foreign key

2009-10-12 Thread Christian Wittwer

Hi Peter,
I forgot to post some important relations in the models.
GameRoster is a table in a m2m relation between Player and Game. I
need some extra attributes on the relationship between player and game
(which position they played).

class GameRoster(models.Model):
player = models.ForeignKey(Player)
game = models.ForeignKey(Game)
POSITION_CHOICES = (
('C', 'Coach'),
('G', 'Goalkeeper'),
('D', 'Defense'),
('O', 'Offense'),
)
position = models.CharField(max_length=1,choices=POSITION_CHOICES)

class Game(models.Model):
players = models.ManyToManyField(Player,
through='GameRoster',blank=True,null=True, related_name='players')

> Why not have
>
> class Player(models.Model):
>    lastname = models.CharField(max_length=60)
>    team = models.ForeignKey(Team)

That won't work, because I need to know which position the player
played at the game.
The position can change from game to game. That's the reason I used
the GameRoster.

Chris

> Christian Wittwer wrote:
>> Hi,
>> I have a question regarding the admin interface of django.
>>
>> My models
>>
>> class Team(models.Model):
>>     name = models.CharField(max_length=10)
>>
>> class Player(models.Model):
>>     lastname = models.CharField(max_length=60)
>>
>> class Goal(models.Model):
>>     game = models.ForeignKey(Game)
>>     goal_scorer =
>> models.ForeignKey(Player,related_name='goal_scorer',blank=True,null=True)
>>
>> class Game(models.Model):
>>     home_team = models.ForeignKey(Team,related_name='home_team')
>>
>> class GameRoster(models.Model):
>>     player = models.ForeignKey(Player)
>>     game = models.ForeignKey(Game)
>>
>> Each Goal is scored by a Player in a Game. I'm using a inline form to
>> insert goals for a game.
>> The players are somehow connected to the team (gameroster), and
>> therefore to the game.
>>
>> I found the place to hook in to limit the choices of player.
>>
>> class GoalInline(admin.TabularInline):
>>     model = Goal
>>     extra = 4
>>
>>     def formfield_for_foreignkey(self, db_field, request, **kwargs):
>>         if db_field.name == "goal_scorer":
>>             kwargs["queryset"] =
>> Player.objects.filter(gameroster__game=self.game)
>>             return db_field.formfield(**kwargs)
>>
>>         return super(GoalInline,
>> self).formfield_for_foreignkey(db_field, request, **kwargs)
>>
>>
>> The missing point is now: How to access game, which is connected to
>> the goal to limit the players?
>> I tried self.game as you can see, but that doesn't work.
>> How can I access the model from within this function?
>>
>> Chris
>>
>> >
>>
>>
>
>
> >
>

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Limit choices based on a foreign key

2009-10-12 Thread Christian Wittwer

Hi,
I have a question regarding the admin interface of django.

My models

class Team(models.Model):
name = models.CharField(max_length=10)

class Player(models.Model):
lastname = models.CharField(max_length=60)

class Goal(models.Model):
game = models.ForeignKey(Game)
goal_scorer =
models.ForeignKey(Player,related_name='goal_scorer',blank=True,null=True)

class Game(models.Model):
home_team = models.ForeignKey(Team,related_name='home_team')

class GameRoster(models.Model):
player = models.ForeignKey(Player)
game = models.ForeignKey(Game)

Each Goal is scored by a Player in a Game. I'm using a inline form to
insert goals for a game.
The players are somehow connected to the team (gameroster), and
therefore to the game.

I found the place to hook in to limit the choices of player.

class GoalInline(admin.TabularInline):
model = Goal
extra = 4

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "goal_scorer":
kwargs["queryset"] =
Player.objects.filter(gameroster__game=self.game)
return db_field.formfield(**kwargs)

return super(GoalInline,
self).formfield_for_foreignkey(db_field, request, **kwargs)


The missing point is now: How to access game, which is connected to
the goal to limit the players?
I tried self.game as you can see, but that doesn't work.
How can I access the model from within this function?

Chris

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Cannot import other model

2009-10-09 Thread Christian Wittwer


> from shcbelpa.season.models import Game
> This is the line of code that generates the error right?
> Is myapp = shcbelpa ?

Yes, exactly. I forgot to replace..
Any idea why the import doesn't work?


> On Fri, 2009-10-09 at 10:39 +0200, Christian Wittwer wrote:
>> Hi,
>> I have a strange problem with importing models from an other app.
>>
>> /myapp/season/models.py
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> ---
>> -
>> from myapp.team.models import Player
>>
>> class Game(models.Model):
>>date_time = models.DateTimeField()
>>
>> class Goal(models.Model):
>>goal_scorer =
>> models.ForeignKey 
>> (Player,related_name='goal_scorer',blank=True,null=True)
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> -
>> Importing Player from the app team works fine.
>>
>> /myapp/team/models.py
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> -
>> from shcbelpa.season.models import Game
>>
>> class Player(models.Model):
>>lastname = models.CharField(max_length=60)
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> --- 
>> -
>>
>> Importing Game from the app team does not work.
>>
>> "ViewDoesNotExist: Could not import myapp.season.views. Error was:
>> cannot import name Game"
>>
>> The view exists and works. As far as I comment out the import of  
>> game,
>> all works fine!
>> In general, every import of models from myapp.season does not work.
>>
>> Why does the import just works in one direction?
>>
>> Cheers,
>> Chris
>>
>>>
>
>
> >

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Cannot import other model

2009-10-09 Thread Christian Wittwer

Hi,
I have a strange problem with importing models from an other app.

/myapp/season/models.py
---
from myapp.team.models import Player

class Game(models.Model):
date_time = models.DateTimeField()

class Goal(models.Model):
goal_scorer =
models.ForeignKey(Player,related_name='goal_scorer',blank=True,null=True)
---
Importing Player from the app team works fine.

/myapp/team/models.py
---
from shcbelpa.season.models import Game

class Player(models.Model):
lastname = models.CharField(max_length=60)
---

Importing Game from the app team does not work.

"ViewDoesNotExist: Could not import myapp.season.views. Error was:
cannot import name Game"

The view exists and works. As far as I comment out the import of game,
all works fine!
In general, every import of models from myapp.season does not work.

Why does the import just works in one direction?

Cheers,
Chris

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Models for a hockey database

2009-10-08 Thread Christian Wittwer

Hi,

> I have a pretty large site that tracks a football team and it's league
> ( http://www.muskegohitmen.com ).

That sounds interesting.. ;-)

> Things you'll probably want to consider
> Seasons - a very small model class, but organizing things by a season
> becomes pretty tricky with out this.
> Rosters ( players per season )
> Staff ( staff Per season )
> Game category ie, pre-season, regular, playoff, championship.
>    * keeping track of win/losses and ties for each game type.
>
> However, my set up track only 1 team which defined by a simply boolean
> check, and can  be changed at any time. I have a thin layer that keeps
> track of win/losses in the league, but not the stats of the whole
> league.  I have a separate league manager application which handles
> things on a league wide level.
> So this may not fit your purpose exactly.

Thx for your input!
I have to track several teams in several leagues, so it's quite tricky.

> I have a trac site set up. If you are are interested in looking over
> the source shoot me an email esatterwhite -[at]- wi.rr.com. I'll set
> you up with a password.

That would be great, I'll write you a e-mail. Maybe I can use some
parts or get some ideas out of your work!

Chris

> On Oct 7, 4:16 pm, Curt  wrote:
>> Instead of including every player in the Game model, I would add a
>> field to the Player model to assign which team a player belongs to:
>>
>> class Team(models.Model):
>>     name = models.CharField(max_length=60)
>>
>> class Player(models.Model):
>>     surname = models.CharField(max_length=60)
>>     lastname = models.CharField(max_length=60)
>>     team = models.ForeignKey(Team)
>>
>> Now you can just specify the home_team and the away_team in the Game
>> model without having to add every player.
>>
>> For more functionality (like allowing players to switch teams) you
>> could use a through table that includes Player, Team, join_date and
>> leave_date -- The example in the docs is a good starting point for
>> that:http://docs.djangoproject.com/en/dev/topics/db/models/
>>
>> I'm not sure how to answer a), but you can figure out b) because the
>> goal_scorer will be assigned to either home_players or away_players in
>> your original Game model, or the goal_scorer will be assigned to the
>> home_team or away_team in the modified models.
>>
>> On Oct 5, 7:21 am, "c!w"  wrote:
>>
>>
>>
>> > Hi,
>> > I trying to create a hockey database, based on Django.
>> > The heaviest part so long is to define the structure of my models.
>> > There I need some help.
>> > What I have so far..
>>
>> > class Team(models.Model):
>> >     name = models.CharField(max_length=60)
>>
>> > class Player(models.Model):
>> >     surname = models.CharField(max_length=60)
>> >     lastname = models.CharField(max_length=60)
>>
>> > class Game(models.Model):
>> >     date_time = models.DateTimeField()
>> >     home_team = models.ForeignKey(Team,related_name='home_team')
>> >     away_team = models.ForeignKey(Team,related_name='away_team')
>> >     home_players = models.ManyToManyField(Player,blank=True)
>> >     away_players = models.ManyToManyField(Player,blank=True)
>>
>> > class GameGoal(models.Model):
>> >     goal_scorer = models.ForeignKey(Player,blank=True,null=True)
>> >     first_assist = models.ForeignKey(Player,blank=True,null=True)
>> >     game = models.ForeignKey(Game)
>>
>> > So now I have the following problems with these models.
>>
>> > a) How can I limit the choice (in the admin page) of goal_scorer to
>> > Players, which are assigned to the Game?
>> > b) A GameGoal should be connected to the home_team or the away_team,
>> > how can I handle this? Add a foreignkey to Team and  limit the choice
>> > to the both teams?
>> > c) Is there a better way, to define such a case?
> >
>

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Models for a hockey database

2009-10-08 Thread Christian Wittwer

Hi,

> class Team(models.Model):
>    name = models.CharField(max_length=60)
>
> class Player(models.Model):
>    surname = models.CharField(max_length=60)
>    lastname = models.CharField(max_length=60)
>    team = models.ForeignKey(Team)
>
> Now you can just specify the home_team and the away_team in the Game
> model without having to add every player.
>
> For more functionality (like allowing players to switch teams) you
> could use a through table that includes Player, Team, join_date and
> leave_date -- The example in the docs is a good starting point for
> that: http://docs.djangoproject.com/en/dev/topics/db/models/

I guess the models ith a through table won't work, because it's quite
complicated.
On one site I have a roster of my team which will be published on the
site. There you see all guys playing at a specific team.
On the other hand I have a game, where some players played, and some note.
So I have to know, which player played, therefore I have to connect
players to a game. You know what I mean?

> I'm not sure how to answer a), but you can figure out b) because the
> goal_scorer will be assigned to either home_players or away_players in
> your original Game model, or the goal_scorer will be assigned to the
> home_team or away_team in the modified models.

Exactly, if I know the player who shoot the goal I know the team he is
playing at.
I think the big part will be the forms to create/add games and players.
They need a lot of logic and some customizations..

Cheers,
Chris

> On Oct 5, 7:21 am, "c!w"  wrote:
>> Hi,
>> I trying to create a hockey database, based on Django.
>> The heaviest part so long is to define the structure of my models.
>> There I need some help.
>> What I have so far..
>>
>> class Team(models.Model):
>>     name = models.CharField(max_length=60)
>>
>> class Player(models.Model):
>>     surname = models.CharField(max_length=60)
>>     lastname = models.CharField(max_length=60)
>>
>> class Game(models.Model):
>>     date_time = models.DateTimeField()
>>     home_team = models.ForeignKey(Team,related_name='home_team')
>>     away_team = models.ForeignKey(Team,related_name='away_team')
>>     home_players = models.ManyToManyField(Player,blank=True)
>>     away_players = models.ManyToManyField(Player,blank=True)
>>
>> class GameGoal(models.Model):
>>     goal_scorer = models.ForeignKey(Player,blank=True,null=True)
>>     first_assist = models.ForeignKey(Player,blank=True,null=True)
>>     game = models.ForeignKey(Game)
>>
>> So now I have the following problems with these models.
>>
>> a) How can I limit the choice (in the admin page) of goal_scorer to
>> Players, which are assigned to the Game?
>> b) A GameGoal should be connected to the home_team or the away_team,
>> how can I handle this? Add a foreignkey to Team and  limit the choice
>> to the both teams?
>> c) Is there a better way, to define such a case?
> >
>

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---