## Usage Examples

### view
```
# Example usage in a DRF ViewSet:
class UserViewSet(OrmOnlyViewMixin, ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def get_queryset(self):
        qs = super().get_queryset()
        return self.hook_orm_fields(qs)
```

### orm service

```
import json
import logging
from common.init.init_django import initialize_django

initialize_django()
from drftokit import OrmOnlyServiceMixin
from apps.user.srv import UserSrv
from apps.user.serializers import UserSerializer


logger = logging.getLogger('debug')


def main():
    # 在 ORM 查询中使用 only，并取出第一个对象用于序列化
    qs = UserSrv.queryset.filter(pk__lt=10)
    qs_o = OrmOnlyServiceMixin.only(serializer_class=UserSerializer, qs=qs)
    logger.info(f"qs_o_query: {qs_o.query}")
    """
    qs_o_query: 
    SELECT 
        `user`.`id`, `user`.`username`, `user`.`nickname`, `user`.`avatar` 
    FROM `user` 
    WHERE (`user`.`deleted` = False AND `user`.`id` < 10) ORDER BY `user`.`id` DESC 
    """

    # s_a = OrmOnlySrv(s=UserSerializer, qs=qs)
    # logger.info(f"data {s_a.data_list}")
    #
    # s_o = OrmOnlySrv(s=UserSerializer, qs=qs)
    # logger.info(f"data {s_o.data}")
    return


if __name__ == '__main__':
    main()
```

### drf list serializer

* orm_only_fields

```
class TplListSerializer(UpdateInfoSerializer, CreatorInfoSerializer, TplSerializer):
    """
    列表: 简化字段，避免复杂逻辑
    """
    class Meta:
        model = Tpl
        orm_only_fields = True
        fields = [
            "id",
            "name",
        ]
```

```
from drftokit import OrmOnlyViewMixin

clsass Viewxxx(OrmOnlyViewMixin):
    def get_queryset(self):
        queryset = self._get_queryset()
        queryset = self.hook_orm_fields(qs=queryset)
        return queryset
```