當前位置: 首頁>>代碼示例>>Python>>正文


Python Serializer.list_to_dict方法代碼示例

本文整理匯總了Python中serializer.Serializer.list_to_dict方法的典型用法代碼示例。如果您正苦於以下問題:Python Serializer.list_to_dict方法的具體用法?Python Serializer.list_to_dict怎麽用?Python Serializer.list_to_dict使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在serializer.Serializer的用法示例。


在下文中一共展示了Serializer.list_to_dict方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from serializer import Serializer [as 別名]
# 或者: from serializer.Serializer import list_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.list_to_dict方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。