title: How to enable filtering on all fields of a model in FastAPI
body: <p>In Django with the restframework, you can do this:</p>
  <pre><code>class Item(models.Model):
      id = models.IntegerField()
      name = models.CharField(max_length=32)
      another_attribute = models.CharField(max_length=32)
      ...
      (more attributes)
      ...
      yet_another_attribute = models.CharField(max_length=32)
  
  class ItemViewSet(viewsets.ReadOnlyModelViewSet):
      permission_classes = [IsAuthenticated]
      serializer_class = ItemSerializer
      filterset_fields = '__all__' # &lt;- this enables filtering on all fields
      queryset = Item.objects.all()
  </code></pre>
  <p>If I want to allow filtering, <code>filterset_fields = '__all__'</code> would allow me to do something like <code>api/item/?(attribute)=(value)</code> and allow me to filter on any attribute</p>
  <p>I'm going through the tutorial (<a href="https://fastapi.tiangolo.com/tutorial/sql-databases/#crud-utils" rel="noreferrer">https://fastapi.tiangolo.com/tutorial/sql-databases/#crud-utils</a>) and it looks like there is a lot of manual filtering involved:</p>
  <pre><code>from fastapi_sqlalchemy import db
  
  class Item(BaseModel):
      id: int
      name: str
      another_attribute: str
      ...
      (more attributes)
      ...
      yet_another_attribute: str
  
  # is it necessary to manually include all the fields I want to filter on as optional query parameters?
  @app.get(&quot;/items/&quot;)
  async def read_item(
      db: Session,
      id: Optional[int] = None,
      name: Optional[str] = None,
      another_attribute: Optional[str] = None,
      ...
      (more attributes)
      ...
      yet_another_attribute: Optional[str] = None
  ):
      # and then I'd need to check if the query parameter has been specified, and if so, filter it.
      queryset = db.session.query(Item)
      if id:
          queryset = queryset.filter(Item.id == id)
      if name:
          queryset = queryset.filter(Item.name == name)
      if another_attribute:
          queryset = queryset.filter(Item.another_attribute == another_attribute)
      ...
      (repeat above pattern for more attributes)
      ...
      if yet_another_attribute:
          queryset = queryset.filter(Item.yet_another_attribute == yet_another_attribute)
  </code></pre>
  <p>What is the preferred way of implementing the above behaviour? Are there any packages that will save me from having to do a lot of manual filtering that will give me the same behaviour as conveniently as the Django Rest Framework viewsets?</p>
  <p>Or is manually including all the fields I want to filter on as optional query parameters, then checking for each parameter and then filtering if present the only way?</p>