开发者

Django Form Models and Editing

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 pla开发者_如何转开发yers 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


You might want to look at select_related.

Here are the docs.


I had to go read your question again, as the question part of it wasn't clear to me. I thought you had a problem of too many queries hitting the database (which may be a problem too and select related will help there), but your question really is:

How to filter a form field to only allow values based on a different field?

Correct? From your question:

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).

In your view, filter the fields on each form by clan. Something like (without your models I can only guess at field names):

form.fields['player'].queryset=form.fields['player'].queryset.filter(clan__in=list_of_selected_clans)

Unrelated suggestion: look at python's string interpolation.

Boa sorte!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜