本文整理匯總了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)