python field readonly_Python serializers.ReadOnlyField方法代码示例
# 需要导入模块: from rest_framework import serializers [as 别名]
# 或者: from rest_framework.serializers import ReadOnlyField [as 别名]
def serializer_factory(endpoint=None, fields=None, base_class=None, model=None):
if model is not None:
assert endpoint is None, "You cannot specify both a model and an endpoint"
from .endpoints import Endpoint
endpoint = Endpoint(model=model)
else:
assert endpoint is not None, "You have to specify either a model or an endpoint"
if base_class is None:
base_class = endpoint.base_serializer
meta_attrs = {
'model': endpoint.model,
'fields': fields if fields is not None else endpoint.get_fields_for_serializer()
}
meta_parents = (object, )
if hasattr(base_class, 'Meta'):
meta_parents = (base_class.Meta, ) + meta_parents
Meta = type('Meta', meta_parents, meta_attrs)
cls_name = '{}Serializer'.format(endpoint.model.__name__)
cls_attrs = {
'Meta': Meta,
}
for meta_field in meta_attrs['fields']:
if meta_field not in base_class._declared_fields:
try:
model_field = endpoint.model._meta.get_field(meta_field)
if isinstance(model_field, OneToOneRel):
cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(read_only=True)
elif isinstance(model_field, ManyToOneRel):
cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
elif isinstance(model_field, ManyToManyRel):
# related ManyToMany should not be required
cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(
many=True,
required=False,
queryset=model_field.related_model.objects.all()
)
except FieldDoesNotExist:
cls_attrs[meta_field] = serializers.ReadOnlyField()
try:
return type(cls_name, (NullToDefaultMixin, base_class, ), cls_attrs)
except TypeError:
# MRO issue, let's try the other way around
return type(cls_name, (base_class, NullToDefaultMixin, ), cls_attrs)
python field readonly_Python serializers.ReadOnlyField方法代码示例相关推荐
- python operator __gt___Python operator.gt方法代码示例
本文整理汇总了Python中operator.gt方法的典型用法代码示例.如果您正苦于以下问题:Python operator.gt方法的具体用法?Python operator.gt怎么用?Pyth ...
- python中weekday_Python calendar.weekday方法代码示例
本文整理汇总了Python中calendar.weekday方法的典型用法代码示例.如果您正苦于以下问题:Python calendar.weekday方法的具体用法?Python calendar. ...
- python中callable_Python abc.Callable方法代码示例
本文整理汇总了Python中collections.abc.Callable方法的典型用法代码示例.如果您正苦于以下问题:Python abc.Callable方法的具体用法?Python abc.C ...
- python使用loaddata_Python data.load方法代码示例
本文整理汇总了Python中data.load方法的典型用法代码示例.如果您正苦于以下问题:Python data.load方法的具体用法?Python data.load怎么用?Python dat ...
- doc python 颜色_Python wordcloud.ImageColorGenerator方法代码示例
本文整理汇总了Python中wordcloud.ImageColorGenerator方法的典型用法代码示例.如果您正苦于以下问题:Python wordcloud.ImageColorGenerat ...
- python asyncio future_Python asyncio.isfuture方法代码示例
本文整理汇总了Python中asyncio.isfuture方法的典型用法代码示例.如果您正苦于以下问题:Python asyncio.isfuture方法的具体用法?Python asyncio.i ...
- python socket connection_Python socket.create_connection方法代码示例
本文整理汇总了Python中gevent.socket.create_connection方法的典型用法代码示例.如果您正苦于以下问题:Python socket.create_connection方 ...
- python中shelf_Python cmds.shelfLayout方法代码示例
本文整理汇总了Python中maya.cmds.shelfLayout方法的典型用法代码示例.如果您正苦于以下问题:Python cmds.shelfLayout方法的具体用法?Python cmds ...
- python中summary_Python summary_pb2.Summary方法代码示例
本文整理汇总了Python中tensorflow.core.framework.summary_pb2.Summary方法的典型用法代码示例.如果您正苦于以下问题:Python summary_pb2 ...
最新文章
- javaSE8的流库总结
- HttpClient basic authentication
- 关于系统重装的一件小事
- AI:2020年6月23日北京智源大会演讲分享之AI创业专题论坛——09:10-10:00 李开复教授《AI赋能时代的创业》
- 如何在 JavaFX 的 TextArea 实现回车发送信息而不换行,但组合键 Ctrl + Enter 换行
- 【最简解法】1048 Find Coins (25 分)_18行代码AC
- hdu 5256 序列变换 (LIS变形)
- (附)python3 只需3小时带你轻松入门——python常用一般性术语或词语的简单解释
- Linux内核调试sysfs
- 初学Phreeze 3
- 使用PInvoke.NET插件为托管代码添加Win32 API签名
- bootstrap入门之Code代码显示
- ubuntu 管理员权限 文件操作
- 数字逻辑电路设计课程设计
- 制造业OEER语言数据挖掘之人工神经网络探索
- vue+tsx+slot
- 微信小程序getUserProfile详解
- 如何将PDF文档进行翻译?PDF文档翻译简单方法介绍
- 移动web开发—— rem布局
- 自动化成本高?那么如何获得最高的投资回报呢?