DRF tokit.
Project description
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 common.serializers.only 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",
]
clsass Viewxxx(OrmOnlyViewMixin):
def get_queryset(self):
queryset = self._get_queryset()
queryset = self.hook_orm_fields(qs=queryset)
return queryset
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
drftokit-0.1.3.tar.gz
(3.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drftokit-0.1.3.tar.gz.
File metadata
- Download URL: drftokit-0.1.3.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88c912d8a828f3b5286da99fbf4a2bd957dccb991b6cdae1d7a3b2560d6d3d5b
|
|
| MD5 |
6996ca5019185cc1b95d45302d25cc48
|
|
| BLAKE2b-256 |
3b1b9fb9de1bd398d914390359b329db8f8ebc22941ff6d5afa2663263d6f251
|
File details
Details for the file drftokit-0.1.3-py3-none-any.whl.
File metadata
- Download URL: drftokit-0.1.3-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
486b732d4461389d7abaf3d349cfa2983938356b7bef595cc908ddc9e174e459
|
|
| MD5 |
26855d4118f7af392d7b6cf2da4f3009
|
|
| BLAKE2b-256 |
29ad6a661eca689f09a1b050a3872e3896e0423f5c0c91d0568ea8a8e2a5e67e
|