Django Admin - Duplicating and Filtering Many to Many fields in form

142 views
Skip to first unread message

Ara Sivaneswaran

unread,
Jan 17, 2014, 7:38:03 AM1/17/14
to django...@googlegroups.com

Hey guys,


I am kind of stuck...

This is the situation. I got a Match Model, a Player Model and a Team model.


Here is what models look like so far:


Match Model (simplified version)

home_team = ForeignKey(Team)

visitor_team = ForeignKey(Team)

result

time

player = ManyToManyField(Player)


Player Model (simplified version)

name

team = ForeignKey(Team)


Team Model (simplified version)

name

Match has many to many relationship with Player (Player particiaptes in a match) Match has a foreign field Home Team Match has a foreign field Visitor Team.

Team has a one to many relationship with Player

What I would like to do is when editing the match, be able to see 2 manytomany fields but filtered by teams. For example, the first manytomany field will only show players from the host team and the second manytomany field will only show players form the visitor team...

I have no idea where to start.... Really stuck...

Thanks a lot, Ara



Tom Evans

unread,
Jan 17, 2014, 9:42:57 AM1/17/14
to django...@googlegroups.com
I would model it differently, then this problem does not occur.

I would have a Roster model. This has a foreign key to a team, and a
many-to-many to the players who played on that team in that game. If
the players have set positions in the game, that would be stored on
the through table to Players for that specific game. Similarly, game
stats would be scored on the through table also.

The match model has two foreign keys to two Rosters (home and away),
the result and time.

Cheers

Tom

Ara Sivaneswaran

unread,
Jan 17, 2014, 11:17:24 AM1/17/14
to django...@googlegroups.com, teva...@googlemail.com
Wow, really nice way to look at it. I would have never thought about it this way!
One little question, is it possible to have all the players already selected in the form? And the admin will remove players who were not part of the game? It's faster to remove 1 or 2 player instead of adding 10-15 players....

Thanks,
Ara
Reply all
Reply to author
Forward
0 new messages