[Answered ]-Trying to get request.user, and then a query, in a form that overrides ModelChoiceField and is subclassed

2👍

You can’t do it that way, because self doesn’t exist at that point – and even if you could, that would be executed at define time, so the rank would be static for all instantiations of the form.

Instead, do it in __init__:

provider = UserModelChoiceField(User.objects.none())

 def __init__(self, user, *args, **kwargs):
    super(NewSwapForm, self).__init__(*args, **kwargs)
    rank = User.objects.get(id=user.id).firefighter_rank_set.get().rank # ??
    self.fields['provider'].queryset = User.objects.order_by('last_name').filter(
           firefighter__hirestatus='active', firefighter_rank__rank=rank)

I’ve put a question mark next to the rank line, because rank_set.get() isn’t valid… not sure what you meant there.

Leave a comment