def save_model(self, request, obj: Booking, form: ModelForm, change: bool): try: with transaction.mark_for_rollback_on_error() as trans: super().save_model(request, obj, form, change) except: form.add_error(NON_FIELD_ERRORS, "The room is already booked on some of those days")
def add_error(self, field, error): """ Safely adds errors. There was an issue where the `cleaned_data` attribute wasn't created after `is_valid` was called. This ensures that the `cleaned_data` attribute is there. """ if getattr(self, "cleaned_data", None) is None or self.cleaned_data is None: self.cleaned_data = {} ModelForm.add_error(self, field, error)
def form_valid(self, form: ModelForm): form.instance.slug = slugify(form.instance.name) try: return super(LocationUpdateView, self).form_valid(form) except InvalidMove: form.add_error( 'parent', ValidationError('World cannot be its own parent!', code='invalid')) return self.form_invalid(form)