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 [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=en
-~----------~----~----~----~------~----~------~--~---