title: Django filter deep related models
body: <p>In my Django 3.2 app, my models have the following relations:</p>
  <pre><code>OperationDraft -&gt; ForeignKey(Structure)
  Structure -&gt; ForeignKey(Customer)
  Customer -&gt; OneToOne(AUTH_USER_MODEL)
  </code></pre>
  <p>Now, when I create a new OperationDrat I have to limit the choice of the structure based on the user of its customer. If the user is not active then I cannot select the structure.</p>
  <p>To achieve that I overrided formfield_for_foreignkey to OperationDraftAdmin class:</p>
  <pre><code>    def formfield_for_foreignkey(self, db_field, request, **kwargs):
          &quot;&quot;&quot;Override formfield_for_foreignkey.&quot;&quot;&quot;
          if db_field.name == &quot;structure&quot;:
              # this doesn't work
              kwargs[&quot;queryset&quot;] = Structure.objects.filter(customer__user__is_active=True)
  
              # this returns 0 results even if we have a customer with such user
              # kwargs[&quot;queryset&quot;] = Structure.objects.filter(customer__user__username=&quot;user480&quot;)
  
          return super().formfield_for_foreignkey(db_field, request, **kwargs)
  </code></pre>
  <p>The filter should be:</p>
  <p><code>kwargs[&quot;queryset&quot;] = Structure.objects.filter(customer__user__is_active=True)</code></p>
  <p>But it returns all the structures, even those ones with a customer bound to an inactive user.</p>
  <p>URL: /operations/operationdraft/add/
  My models (limited to relevant code):</p>
  <pre><code>@admin.register(OperationDraft)
  class OperationDraftAdmin(OperationProxyAdmin):
      &quot;&quot;&quot;Set OperationDraft Admin.&quot;&quot;&quot;
  
      def formfield_for_foreignkey(self, db_field, request, **kwargs):
          &quot;&quot;&quot;Override formfield_for_foreignkey.&quot;&quot;&quot;
          if db_field.name == &quot;structure&quot;:
              # this doesn't work
              kwargs[&quot;queryset&quot;] = Structure.objects.filter(customer__user__is_active=True)
  
              # this returns 0 results even if we have a customer with such user
              # kwargs[&quot;queryset&quot;] = Structure.objects.filter(customer__user__username=&quot;user480&quot;)
  
              # this works but it's not what I need
              # kwargs[&quot;queryset&quot;] = Structure.objects.filter(customer__name=&quot;Customer249&quot;)
  
          return super().formfield_for_foreignkey(db_field, request, **kwargs)
  
  
  class Operation(models.Model, StatusLogMixin):
      structure = models.ForeignKey(
          Structure,
          verbose_name=&quot;struttura&quot;,
          related_name=&quot;operations&quot;,
          on_delete=models.PROTECT,
      )
  
  class OperationDraft(Operation):
      &quot;&quot;&quot;Define draft operation proxy model.&quot;&quot;&quot;
  
      class Meta:
          &quot;&quot;&quot;Define model Meta.&quot;&quot;&quot;
  
          proxy = True
          verbose_name = &quot;Intervento in bozza&quot;
          verbose_name_plural = &quot;01 - Inserimento Interventi&quot;
  
  
  class Customer(models.Model):
      &quot;&quot;&quot;Customer model.&quot;&quot;&quot;
      user = models.OneToOneField(
          settings.AUTH_USER_MODEL,
          verbose_name=&quot;utente web&quot;,
          on_delete=models.SET_NULL,
          related_name=&quot;customer&quot;,
          blank=True,
          null=True,
          limit_choices_to={&quot;groups__name&quot;: settings.CUSTOMER},
      )
  
  class Structure(models.Model):
      &quot;&quot;&quot;Model of the Structure.&quot;&quot;&quot;
  
      customer = models.ForeignKey(
          Customer,
          verbose_name=&quot;cliente&quot;,
          related_name=&quot;structures&quot;,
          on_delete=models.PROTECT,
      )
  </code></pre>
  <p>I checked <a href="https://docs.djangoproject.com/en/3.2/topics/db/queries/#related-objects" rel="nofollow noreferrer">documentation</a> and to me my query is correct but not working.</p>
  <p>What am I doing wrong?</p>