当前位置: 首页>>代码示例>>Python>>正文


Python Serializer.to_dict方法代码示例

本文整理汇总了Python中serializer.Serializer.to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.to_dict方法的具体用法?Python Serializer.to_dict怎么用?Python Serializer.to_dict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在serializer.Serializer的用法示例。


在下文中一共展示了Serializer.to_dict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import to_dict [as 别名]
class ApiController:
    def __init__(self, model, **kwargs):
        self.model = model
        self.fields = kwargs.get("fields") or self.model._meta.get_all_field_names()
        self.name = kwargs.get("name")
        self.parent_name = kwargs.get("parent_name", False)
        if self.parent_name:
            self.parent_id = self.parent_name + "_id"
        else:
            self.parent_id = ""
        self.id = self.name + "_id"
        self.set_name = self.name + "_set"
        self.relations = kwargs.get("relations") or [
            rel.get_accessor_name() for rel in model._meta.get_all_related_objects()
        ]
        self.parent_model = kwargs.get("parent_model")
        self.serialize = Serializer(self.fields)

    @csrf_exempt
    def nested_all(self, request, **kwargs):
        if request.method == "POST":
            return run_response(self.create, request, kwargs)
        else:
            return run_response(self.nested_list, request, kwargs)

    @csrf_exempt
    def all(self, request, **kwargs):
        if request.method == "POST":
            return run_response(self.create, request, kwargs)
        else:
            return run_response(self.list, request, kwargs)

    @csrf_exempt
    def all_or_authenticate(self, request, **kwargs):
        if request.method == "POST":
            return run_response(self.create_user, request, kwargs)
        else:
            return run_response(self.list, request, kwargs)

    @csrf_exempt
    def single(self, request, **kwargs):
        if request.method == "POST" or request.method == "PATCH" or request.method == "PUT":
            return run_response(self.update, request, kwargs)
        elif request.method == "DELETE":
            return run_response(self.destroy, request, kwargs)
        else:
            return run_response(self.show, request, kwargs)

    def create_user(self, request, params):
        attrs = self.serialize.to_dict(request.data)
        if params.get(self.parent_id):
            attrs[self.parent_id] = params[self.parent_id]
        user = self.model(**attrs)
        user.save()
        token = jwt.encode({"user_id": user.id}, SECRET_KEY)
        return {"success": True, "token": token, "user": self.serialize.model_to_dict(user)}

    def create(self, request, params):
        attrs = self.serialize.to_dict(request.data)
        if params.get(self.parent_id):
            attrs[self.parent_id] = params[self.parent_id]
        record = self.model(**attrs)
        record.save()
        return self.serialize.model(record)

    def update(self, request, params):
        filtered = self.model.objects.filter(pk=params[self.id])
        filtered.update(**self.serialize.to_dict(request.data))
        return self.serialize.model(filtered[0])

    def destroy(self, request, params):
        record = self.model.objects.get(pk=params[self.id])
        record.delete()
        return {"status": "success", "message": "Record deleted."}

    def list(self, request, params):
        records = self.model.objects.all()
        return self.serialize.list(records)

    def nested_list(self, request, params):
        getattr(self.parent_model, self.set_name)
        parent = self.parent_model.objects.get(pk=params.get(self.parent_id))
        records = getattr(parent, self.set_name).all()
        return self.serialize.list(records)

    def show(self, request, params):
        record = self.model.objects.get(pk=params[self.id])
        relations = self.add_relations(record)
        return self.serialize.model(record, relations)

    def add_relations(self, record):
        relations = {}
        print >>sys.stderr, "relations", self.relations
        if self.relations:
            for name in self.relations:
                relations[name] = self.serialize.list_to_dict(getattr(record, name).all())
                print >>sys.stderr, "DATA:", "add relations", getattr(record, name).all()
        print >>sys.stderr, "relations", relations
        return relations
开发者ID:ColDog,项目名称:django-api,代码行数:101,代码来源:views.py


注:本文中的serializer.Serializer.to_dict方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。