我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.db.models.ProtectedError()。
def delete(self, *args, **kwargs): """Deletes a user instance. Trying to delete a meta user raises the `ProtectedError` exception. """ if self.is_meta: raise ProtectedError('Cannot remove meta user instances', None) purge = kwargs.pop("purge", False) if purge: UserPurger(self).purge() else: UserMerger(self, User.objects.get_nobody_user()).merge() super(User, self).delete(*args, **kwargs)
def delete_reverse_relations_if_need(self, instance, reverse_relations): nested_related_kwarg = getattr(self.Meta, 'nested_related_kwarg') # Reverse `reverse_relations` for correct delete priority reverse_relations = OrderedDict( reversed(list(reverse_relations.items()))) # Delete instances which is missed in data for field_name, field in reverse_relations.items(): Model = field.Meta.model current_ids = [d.get('pk') for d in self.initial_data[field_name]] try: Model.objects.filter( **{nested_related_kwarg: instance} ).exclude(pk__in=current_ids).delete() except ProtectedError as e: instances = e.args[1] self.fail('cannot_delete_protected', instances=", ".join([ instance.__str__() for instance in instances]))
def delete_pool(self, vici): vici_poolname = {'name': self.poolname} try: vici.unload_pool(vici_poolname) self.pool.delete() messages.add_message(self.request, messages.SUCCESS, "Pool deletion successful.") except ViciException as e: messages.add_message(self.request, messages.ERROR, 'Unload pool failed: ' + str(e)) except ProtectedError: messages.add_message(self.request, messages.ERROR, 'Pool not deleted. Pool is in use by a connection.') except Exception as e: messages.add_message(self.request, messages.ERROR, str(e)) return redirect(reverse("pools:index"))
def collect(self, objs, source=None, source_attr=None, **kwargs): for obj in objs: if source_attr and not source_attr.endswith('+'): related_name = source_attr % { 'class': source._meta.model_name, 'app_label': source._meta.app_label, } self.add_edge(getattr(obj, related_name), obj) else: self.add_edge(None, obj) self.model_objs[obj._meta.model].add(obj) try: return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs) except models.ProtectedError as e: self.protected.update(e.protected_objects)
def delete(self, request, *args, **kwargs): """Delete the model and return its JSON representation.""" if self.pk_field_name not in kwargs: return self.status_msg('DELETE is not supported for collections', status=405) obj = self.get_object() try: obj.delete() return JsonResponse({}) except ProtectedError as e: return self.status_msg(e[0], status=405)
def collect(self, objs, source_attr=None, **kwargs): for obj in objs: if source_attr and hasattr(obj, source_attr): self.add_edge(getattr(obj, source_attr), obj) else: self.add_edge(None, obj) try: return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs) except models.ProtectedError as e: self.protected.update(e.protected_objects)
def collect(self, objs, source_attr=None, **kwargs): for obj in objs: if source_attr and hasattr(obj, source_attr): self.add_edge(getattr(obj, source_attr), obj) else: self.add_edge(None, obj) try: return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs) except models.ProtectedError, e: self.protected.update(e.protected_objects)
def destroy(tag_id): """ Remove tag """ try: tag = Tag.objects.get(id=tag_id) except (ObjectDoesNotExist, ValueError): raise NotFound('Tag not found') try: tag.delete() return {'message': 'Tag successfully removed'} except ProtectedError: raise Forbidden('Tag still referenced in reports/tickets')
def destroy(news_id): """ Remove news """ try: news = News.objects.get(id=news_id) except (ObjectDoesNotExist, ValueError): return NotFound('News not found') try: news.delete() return {'message': 'News successfully removed'} except ProtectedError: raise Forbidden('News still referenced in reports/tickets')
def destroy(cat): """ Remove category """ try: category = Category.objects.get(name=cat) except (ObjectDoesNotExist, ValueError): raise NotFound('Category not found') try: category.delete() return {'message': 'Category successfully removed'} except ProtectedError: raise Forbidden('Category still referenced in reports/tickets')
def destroy(template_id): """ Remove email templates """ template = None try: template = MailTemplate.objects.get(id=template_id) except (ObjectDoesNotExist, ValueError): raise NotFound('Template not found') try: template.delete() return {'message': 'Email template successfully removed'} except ProtectedError: raise Forbidden('Mail template still referenced in reports/tickets')
def destroy(prov): """ Remove provider """ try: provider = Provider.objects.filter(email=prov) except (ObjectDoesNotExist, ValueError): raise NotFound('Provider not found') try: provider.delete() return {'message': 'Provider successfully removed'} except ProtectedError: raise Forbidden('Provider still referenced in reports')
def collect(self, objs, source=None, source_attr=None, **kwargs): for obj in objs: if source_attr and not source_attr.endswith('+'): related_name = source_attr % { 'class': source._meta.model_name, 'app_label': source._meta.app_label, } self.add_edge(getattr(obj, related_name), obj) else: self.add_edge(None, obj) self.model_count[obj._meta.verbose_name_plural] += 1 try: return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs) except models.ProtectedError as e: self.protected.update(e.protected_objects)
def collect(self, objs, source_attr=None, **kwargs): for obj in objs: if source_attr: self.add_edge(getattr(obj, source_attr), obj) else: self.add_edge(None, obj) try: return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs) except models.ProtectedError, e: self.protected.update(e.protected_objects)
def collect(self, objs, source_attr=None, **kwargs): for obj in objs: if source_attr: self.add_edge(getattr(obj, source_attr), obj) else: self.add_edge(None, obj) try: return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs) except models.ProtectedError as e: self.protected.update(e.protected_objects)
def destroy(self, request, *args, **kwargs): instance = self.get_object() try: instance.delete() return Response(status=status.HTTP_204_NO_CONTENT) except ProtectedError: msg = 'Cannot be deleted due to protected related entities' return Response(data=msg, status=status.HTTP_400_BAD_REQUEST)
def test_protect_student(self): semester = self.SemesterFactory.create() self.StudentFactory.create(matriculation_semester=semester) with self.assertRaises(models.ProtectedError): semester.delete()