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


Python serializers.DateTimeField方法代碼示例

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


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

示例1: add_serializer

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import DateTimeField [as 別名]
def add_serializer(table):
    fields = table.fields.all()
    attributes = {}
    for field in fields:
        args = {
            "label": field.alias
        }
        if not field.required:
            args["default"] = None
            args["allow_null"] = True
        if field.type == 3:
            args["format"] = "%Y-%m-%dT%H:%M:%S"
        elif field.type == 6:
            args["protocol"] = "IPv4"
        f = FIELD_TYPE_MAP[field.type](**args)
        if(field.is_multi):
            attributes[field.name] = serializers.ListField(default=[], child=f)
        else:
            attributes[field.name] = f
        # if(field.type == 0):
        #     attributes["validate_{}".format(field.name)] = empty_none
    #創建者拿到視圖aQ!
    # attributes["S-creator"] = serializers.CharField(read_only=True, default=serializers.CurrentUserDefault())
    attributes["S-creation-time"] = serializers.DateTimeField(read_only=True, format="%Y-%m-%dT%H:%M:%S",
                                                              default=datetime.datetime.now)
    attributes["S-last-modified"] = serializers.CharField(default=None, allow_null=True, read_only=True, label="最後修改人")
    serializer = type(table.name, (Serializer, ), attributes)
    setattr(app_serializers, table.name, serializer) 
開發者ID:open-cmdb,項目名稱:cmdb,代碼行數:30,代碼來源:initialize.py

示例2: update

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import DateTimeField [as 別名]
def update(self, request, *args, **kwargs):
        try:
            res = es.get(index="test_12", doc_type="one", id=kwargs["pk"])
        except NotFoundError as exc:
            raise exceptions.NotFound("Document {} was not found in Type one of Index test_12".format(kwargs["pk"]))
        except Exception as exc:
            raise exceptions.APIException("內部錯誤, 錯誤類型:{}".format(type(exc)))
        his_data = res["_source"]
        partial = kwargs.get("partial", False)
        serializer = self.get_serializer(data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)

        data = copy.copy(his_data)
        is_equal = True
        for k,v in serializer.validated_data.items():
            if k[0] == "S":
                continue
            if isinstance(serializer.fields.fields[k], serializers.DateTimeField):
                if isinstance(v, list):
                    v = list(map(lambda x: x.isoformat(), v))
                else:
                    v = v.isoformat()
            data[k] = v
            if his_data[k] != v:
                is_equal = False
                break
        if is_equal:
            raise exceptions.ParseError(detail="No field changes")
        his_data.pop("S_creator")
        his_data.pop("S_creation_time")
        his_data["S_data_id"] = kwargs["pk"]
        his_data["S_changer"] = request.user.username
        his_data["S_update_time"] = datetime.datetime.now()
        try:
            es.index(index="test_22", doc_type="one", id=uuid.uuid1(), body=his_data)
            res = es.index(index="test_12", doc_type="one", id=kwargs["pk"], body=data)
        except Exception as exc:
            raise exceptions.APIException("內部錯誤,錯誤類型: {}".format(type(exc)))
        return Response(res) 
開發者ID:open-cmdb,項目名稱:cmdb,代碼行數:41,代碼來源:views.py

示例3: test_should_date_time_convert_datetime

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import DateTimeField [as 別名]
def test_should_date_time_convert_datetime():
    assert_conversion(serializers.DateTimeField, graphene.types.datetime.DateTime) 
開發者ID:graphql-python,項目名稱:graphene-django,代碼行數:4,代碼來源:test_field_converter.py

示例4: __init__

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import DateTimeField [as 別名]
def __init__(self, allow_extra):
        class Model(serializers.Serializer):
            id = serializers.IntegerField()
            client_name = serializers.CharField(max_length=255, trim_whitespace=False)
            sort_index = serializers.FloatField()
            # client_email = serializers.EmailField(required=False, allow_null=True)
            client_phone = serializers.CharField(max_length=255, trim_whitespace=False, required=False, allow_null=True)

            class Location(serializers.Serializer):
                latitude = serializers.FloatField(required=False, allow_null=True)
                longitude = serializers.FloatField(required=False, allow_null=True)
            location = Location(required=False, allow_null=True)

            contractor = serializers.IntegerField(required=False, allow_null=True, min_value=0)
            upstream_http_referrer = serializers.CharField(
                max_length=1023, trim_whitespace=False, required=False, allow_null=True
            )
            grecaptcha_response = serializers.CharField(min_length=20, max_length=1000, trim_whitespace=False)
            last_updated = serializers.DateTimeField(required=False, allow_null=True)

            class Skill(serializers.Serializer):
                subject = serializers.CharField()
                subject_id = serializers.IntegerField()
                category = serializers.CharField()
                qual_level = serializers.CharField()
                qual_level_id = serializers.IntegerField()
                qual_level_ranking = serializers.FloatField(default=0)
            skills = serializers.ListField(child=Skill())

        self.allow_extra = allow_extra  # unused
        self.serializer = Model 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:33,代碼來源:test_drf.py

示例5: get_text

# 需要導入模塊: from rest_framework import serializers [as 別名]
# 或者: from rest_framework.serializers import DateTimeField [as 別名]
def get_text(self, instance):
        """
        Haystack facets are returned as a two-tuple (value, count).
        The text field should contain the faceted value.
        """
        instance = instance[0]
        if isinstance(instance, (six.text_type, six.string_types)):
            return serializers.CharField(read_only=True).to_representation(instance)
        elif isinstance(instance, datetime):
            return serializers.DateTimeField(read_only=True).to_representation(instance)
        return instance 
開發者ID:rhblind,項目名稱:drf-haystack,代碼行數:13,代碼來源:serializers.py


注:本文中的rest_framework.serializers.DateTimeField方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。