-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: UTF-8 -*- | ||
""" | ||
@author: hhyo | ||
@license: Apache Licence | ||
@file: __init__.py | ||
@time: 2022/10/22 | ||
""" | ||
__author__ = "hhyo" | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class BaseModelSerializer(serializers.ModelSerializer): | ||
"""BaseModelSerializer,主要是引入过滤和排除字段的方法""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
""" | ||
``fields`` 需要保留的字段列表 | ||
``exclude`` 需要排除的字段列表 | ||
""" | ||
fields = kwargs.pop("fields", None) | ||
exclude = kwargs.pop("exclude", None) | ||
super(BaseModelSerializer, self).__init__(*args, **kwargs) | ||
|
||
for field_name in set(self.fields.keys()): | ||
if not any([fields, exclude]): | ||
break | ||
if fields and field_name in fields: | ||
continue | ||
if exclude and field_name not in exclude: | ||
continue | ||
self.fields.pop(field_name, None) | ||
|
||
@staticmethod | ||
def setup_eager_loading(queryset): | ||
""" | ||
Perform necessary eager loading of data. | ||
https://ses4j.github.io/2015/11/23/optimizing-slow-django-rest-framework-performance/ | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters