本文整理汇总了Python中django.forms.util.ErrorDict.setdefault方法的典型用法代码示例。如果您正苦于以下问题:Python ErrorDict.setdefault方法的具体用法?Python ErrorDict.setdefault怎么用?Python ErrorDict.setdefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.util.ErrorDict
的用法示例。
在下文中一共展示了ErrorDict.setdefault方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RentalReservationLineEditItemForm
# 需要导入模块: from django.forms.util import ErrorDict [as 别名]
# 或者: from django.forms.util.ErrorDict import setdefault [as 别名]
class RentalReservationLineEditItemForm(forms.ModelForm):
class Meta:
model = RentalReservationLineItem
fields = ("product", "qty")
def clean_qty(self):
qty = self.cleaned_data.get("qty")
if qty <= 0:
raise ValidationError("Reservation qty must be greater than 0")
return qty
def validate_qty(self, facility, start_time, end_time):
qty = self.instance.qty
product = self.instance.product
# if form corresponds to existing ReservationLineItem being changed, get previous scheduled_adjustment_id
existing_adjustment_id = self.instance.pk or None
# have to ignore previous scheduled_adjustment qty when determining if sufficient inventory exists
min_inventory_during_reservation = \
facility.get_min_expected_inventory_in_interval(product.id,
start_time,
end_time,
scheduled_adjustment_exclude_id=existing_adjustment_id)
if min_inventory_during_reservation - qty < 0:
if not self._errors:
self._errors = ErrorDict()
qty_errors = self._errors.setdefault("qty", ErrorList())
expanded_search_buffer = timedelta(hours=5)
inventory_search_start = start_time - expanded_search_buffer
inventory_search_end = end_time + expanded_search_buffer
expected_inventory_url = expected_inventory_url_with_querystring(product, facility, inventory_search_start, inventory_search_end)
error_message = mark_safe("Only expected to have {} available at reservation time. Want to <a href='{}' target='_blank'>check</a> when more will be available?".format(min_inventory_during_reservation, expected_inventory_url))
qty_errors.append(error_message)