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
Metadata
Release files for drftokit 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drftokit-0.1.7.tar.gz | 3.6 kB | Without provenance | Details for drftokit-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drftokit-0.1.7-py3-none-any.whl | Python 3 | none | any | Details for drftokit-0.1.7-py3-none-any.whl |
Total release size: 7.2 kB
Release files / drftokit-0.1.7.tar.gz
| Download URL | drftokit-0.1.7.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
13157c50c1642728b71837fdea36f1d8f2714ba7ca986ae66b3bb7edc8ca0196
|
|
BLAKE2b-256 checksum How to use checksums |
3db889c001e76a189c8c92a7cae52f796448b7954e15911a81cf58f1f537b5a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.8
|
Release files / drftokit-0.1.7-py3-none-any.whl
| Download URL | drftokit-0.1.7-py3-none-any.whl |
|---|---|
| Size | 3.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
71b34dfdff3b03a401f13c6498a83bb3cc2beac440208e7c4e1afae4155e8250
|
|
BLAKE2b-256 checksum How to use checksums |
b35efd2bc585fe9a1ea5250c691d16b13c3033d8c1cc35f3abdeafd5d5f9160f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.8
|
Security for drftokit 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.