# 需要导入模块: 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方法代码示例相关推荐

  1. python operator __gt___Python operator.gt方法代码示例

    本文整理汇总了Python中operator.gt方法的典型用法代码示例.如果您正苦于以下问题:Python operator.gt方法的具体用法?Python operator.gt怎么用?Pyth ...

  2. python中weekday_Python calendar.weekday方法代码示例

    本文整理汇总了Python中calendar.weekday方法的典型用法代码示例.如果您正苦于以下问题:Python calendar.weekday方法的具体用法?Python calendar. ...

  3. python中callable_Python abc.Callable方法代码示例

    本文整理汇总了Python中collections.abc.Callable方法的典型用法代码示例.如果您正苦于以下问题:Python abc.Callable方法的具体用法?Python abc.C ...

  4. python使用loaddata_Python data.load方法代码示例

    本文整理汇总了Python中data.load方法的典型用法代码示例.如果您正苦于以下问题:Python data.load方法的具体用法?Python data.load怎么用?Python dat ...

  5. doc python 颜色_Python wordcloud.ImageColorGenerator方法代码示例

    本文整理汇总了Python中wordcloud.ImageColorGenerator方法的典型用法代码示例.如果您正苦于以下问题:Python wordcloud.ImageColorGenerat ...

  6. python asyncio future_Python asyncio.isfuture方法代码示例

    本文整理汇总了Python中asyncio.isfuture方法的典型用法代码示例.如果您正苦于以下问题:Python asyncio.isfuture方法的具体用法?Python asyncio.i ...

  7. python socket connection_Python socket.create_connection方法代码示例

    本文整理汇总了Python中gevent.socket.create_connection方法的典型用法代码示例.如果您正苦于以下问题:Python socket.create_connection方 ...

  8. python中shelf_Python cmds.shelfLayout方法代码示例

    本文整理汇总了Python中maya.cmds.shelfLayout方法的典型用法代码示例.如果您正苦于以下问题:Python cmds.shelfLayout方法的具体用法?Python cmds ...

  9. python中summary_Python summary_pb2.Summary方法代码示例

    本文整理汇总了Python中tensorflow.core.framework.summary_pb2.Summary方法的典型用法代码示例.如果您正苦于以下问题:Python summary_pb2 ...

最新文章

  1. javaSE8的流库总结
  2. HttpClient basic authentication
  3. 关于系统重装的一件小事
  4. AI:2020年6月23日北京智源大会演讲分享之AI创业专题论坛——09:10-10:00 李开复教授《AI赋能时代的创业》
  5. 如何在 JavaFX 的 TextArea 实现回车发送信息而不换行,但组合键 Ctrl + Enter 换行
  6. 【最简解法】1048 Find Coins (25 分)_18行代码AC
  7. hdu 5256 序列变换 (LIS变形)
  8. (附)python3 只需3小时带你轻松入门——python常用一般性术语或词语的简单解释
  9. Linux内核调试sysfs
  10. 初学Phreeze 3
  11. 使用PInvoke.NET插件为托管代码添加Win32 API签名
  12. bootstrap入门之Code代码显示
  13. ubuntu  管理员权限 文件操作
  14. 数字逻辑电路设计课程设计
  15. 制造业OEER语言数据挖掘之人工神经网络探索
  16. vue+tsx+slot
  17. 微信小程序getUserProfile详解
  18. 如何将PDF文档进行翻译?PDF文档翻译简单方法介绍
  19. 移动web开发—— rem布局
  20. 自动化成本高?那么如何获得最高的投资回报呢?

热门文章

  1. 项望烽:移动IM开发那些事儿
  2. SEO算法:如何通过PageRank算法判断SEO排序结果
  3. String与Date转换
  4. 知识点的积累(零碎的积累)
  5. HDU 1724 Ellipse ——Simpson积分
  6. 使用xshell 登陆aws的ec2
  7. 1.4 VC6.0在win7下安装的兼容性问题以及解决办法
  8. 过虑数字类型后面的0
  9. 宝塔php7.1地址在哪里,宝塔Linux面板安全入口地址忘了(方法一)
  10. Java poi插件导出Excel文件合并多sheet页