我正在向我的 Django Rest Framework 后臺發送一個表單,
。我正在使用一個 DateField,它是可選的,取決于用戶正在查看的表單型別。
當用戶可以看到 DateField 并選擇日期時,它可以適當地提交,并且沒有任何問題。
當 DateField 被隱藏時,我在提交表單時得到了一個格式化錯誤(但不應該)
。當日期是可見的:
當日期是可見的時候
<div class="mt-2 text-center">
< input type="hidden" name="summative" value="127" id="id_summative">
< input type="hidden" name="evaluation_type" value="31" id="id_evaluation_type">
< div id="id_evaluation_levelFormGroup" class="form-group">
< label class="form-label" for="id_evaluation_level"> 評價水平</label>。
< select name="valuation_level" class="form- control" required="" id="id_evaluation_level">
<option value="" selected=">> ---------</option>
<option value="41"/span>> 正常</option>>
<option value="42"/span>> 正式的</option>。
</select>/span>
</div>/span>
< div id="id_evaluation_dateFormGroup" class="form-group">
< label class="form-label" for="id_evaluation_date"> 評估日期</label>。
< input type="date"/span> name="evaluation_date" value="2021-09-08" class="form-control" id="id_evaluation_date">
</div>
</div>
當HIDDEN時:
<div class="mt-2 text-center"/span>>
< input type="hidden"/span> name="summative" value="127" id="id_summative">
< input type="hidden" name="evaluation_type" value="33" id="id_evaluation_type">
< input type="hidden" name="evaluation_level" value="43" id="id_evaluation_level">
< input type="hidden"/span> name="evaluation_date"/span> id="id_evaluation_date">
</div>
evaluation_date不是一個必填欄位,模型屬性看起來像這樣:
evaluation_date = models.DateField(
auto_now=False, auto_now_add=False, null=True, blank=True)
)
我的序列化器看起來像這樣(包括日期的驗證方法):
class EvaluationSerializer(serializers.ModelSerializer)。
def validate_evaluation_date(self, attrs)。
# 獲取日期字串。
date_string = self.context["request"].data["evaluation_date"]
# 獲取型別。
evaluation_type_id = self.context["request"].data["evaluation_type"]
evaluation_type = EvaluationType.objects.get(id=evaluation_type_id)
# If we have an Observation, blanks should not be allowed[/span].
if evaluation_type.name == "Observation" and attrs == None:
raise serializers.ValidationError("This field is required.")
if evaluation_type.name == "Observation" and attrs == " ":
raise serializers.ValidationError("This field is required.")
# 將字串決議為一個日期物件。
date = datetime.strptime(date_string, "%Y-%m-%d").date()
回傳日期
absolute_url = serializers.SerializerMethodField()
created_by = serializers.HiddenField()
default=serializers.CurrentUserDefault()
)
updated_by = serializers.HiddenField(
默認=serializers.CurrentUserDefault()
)
evidences = serializers.SerializerMethodField()
requestor_can_add = serializers.SerializerMethodField()
subdomains = serializers.SerializerMethodField()
evaluation_date = serializers.DateField()
class Meta:
模型 = 評價
欄位 = [...
"url",
"absolute_url"。
"id"。
"summative"。
"employee_locked"。
"admin_locked"。
"admin_locked_by_id"。
"published"。
"evidence"。
"subdomains"。
"requestor_can_add"。
"evaluation_type"。
"evaluation_level"。
"evaluation_date"。
"created_by"。
"uped_by"。
]
在我的設定中,我的輸入格式是這樣設定的:
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS"/span>。"rest_framework.pagination.LimitOffsetPagination"。
"DATETIME_FORMAT": "%m/%d/%Y - %I:%M:%S %p"。
"DATE_INPUT_FORMATS": ["%Y-%m-%d"],
"DEFAULT_AUTHENTICATION_CLASSES": [
# Enabling this it will require Django Session (Including CSRF).
"rest_framework.authentication.SessionAuthentication"。
],
"DEFAULT_PERMISSION_CLASSES"/span>: [
# 全球范圍內只允許IsAuthenticated用戶訪問API端點。
"rest_framework.permissions.IsAuthenticated"。
],
}
問題是這樣的--當我提交表單時(當輸入被隱藏且evaluation_date應該是空白時,它給出了以下錯誤:
日期有錯誤的格式。使用這些格式中的一種來代替。YYYY-MM-DD.
但是這個值應該是空白的(正如你從上面的HTML中可以看到的)
uj5u.com熱心網友回復:
問題是你在你的序列化器中宣告了evaluation_date:
evaluation_date = serializers.DateField()
因為這個欄位是必需的。只要從序列化器中洗掉這個欄位,它就可以作業了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/310910.html
標籤:
