M. Gar
M. Gar

Reputation: 887

ModelChoiceField Queryset to another OneToOne related table

I have one ModelChoiceFiled to show the users in User and use queryset to filter the users something like this:

forms.py
class Form(forms.Form):
    user = ModelChoiceField(queryset = User.objects.filter(is_staff=True), empty_label='Select the user', label='User')

I have another Model that have a OneToOneField to the User model. Is a profile Model:

models.py
class Profile(models.Model):
    user = models.OneToOneField(User)
    fieltocheck = models.BooleanField()

But now I need that the ModelChoiceField check also that the field fieldtocheck in the Profile model be False something like queryset = User.objects.filter(is_staff=True, fieldtocheck=False) I think I can solve it using User.objects.select_related() but I don't get how can I do it.

Upvotes: 1

Views: 160

Answers (1)

Antoine Pinsard
Antoine Pinsard

Reputation: 35002

What about:

user = ModelChoiceField(queryset=User.objects.filter(
    is_staff=True, profile__fieldtocheck=False), ...)

Upvotes: 1

Related Questions