Wow, I'm having such a hard time on this. I must be doing something wrong, I'm getting an incredible ammount of queries.
So, my models are the following: Player Clan Match (as in game match) MatchMap (the maps played of a match) MatchPlayer (the players of a match)
All of them are related via foreign key, no m2m relationship. A player can be in a clan and a match involves 2 clans. A match can have any ammount of maps and only players from the two clans involved can be in the match (although on the future the players might not be in the same clan they played on that match, so I specify the side they played on the match).
So, I made the submit match, all ok. But to edit this info, it's caos!
For editing MatchPlayers of a match I tried using inlineformset_factory
PlayersFormSet = inlineformset_factory(MatchBetweenClans, MatchPlayer)
playersForms = PlayersFormSet(instance=match)
This already starts bad because for each instance of player on a match, Django hits the database and gets a list of all players. If for example 6 players are on a match, and I have 2 empty forms provived by the inlineformset_factory
, I see on the log
SELECT
...
FROM
`accounts_customuser`
8 times.
Now, even if that worked correctly for me, it does not do what I need. When adding players to a match, not all players should be on the list, only those from the 2 specified clans, ideally as the form of checkboxes (each checkbox being a player of a clan). When submitting the match this is easy to do:
clan1PlayerList = CustomUser.objects.filter(clan=clan1Instance)
clan2PlayerList = CustomUser.objects.filter(clan=clan2Instance)
playersClan1 = forms.ModelMultipleChoiceField(queryset=clan1PlayerList, label="Jogadores - "+clan1Instance.tag+"", widget=forms.CheckboxSelectMultiple(attrs={'class':'input_checkbox'}))
playersClan2 = forms.ModelMultipleChoiceField(queryset=clan2PlayerList, label="Jogadores - "+clan2Instance.tag, widget=forms.CheckboxSelectMultiple(attrs={'class':'input_checkbox'}))
Is there anyway I could have this on a formulary to be edited? I can't find a way to send the playerlist of a clan and the currect players of a match to a form. Thanks