模型对象没有从内部或在提出异常之前从内部或直接创建的模型对象

发布于 2025-02-11 05:03:19 字数 1169 浏览 1 评论 0原文

我的Django应用程序正在发生一些非常奇怪的事情。当我尝试在提出异常之前运行以下代码

Model.objects.create(**obj)

或在块中运行相同的代码时,该代码在没有任何问题的情况下运行,但是我看不到数据库中的对象。运行Model.Objects.all()也不会返回任何内容。

这是完整的代码:

def has_permission(self, request, view):
        try:
            serializer = VerifyOTPRequestSerializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            otp = request.data.pop("otp")
            flow = request.data.pop("flow")
            primary_phone = request.data.get("contact_detail", {}).get(
                "primary_phone", ""
            )            
            if OTPFailedAttempts.objects.filter(phone=primary_phone).count() > 0:
                raise Exception("otpAttemptError")
            
            return helpers.OTPHelper.verify_otp(primary_phone, otp, flow)
        except Exception as permission_error:
            
            OTPFailedAttempts.objects.create(**{"phone": primary_phone, 'email': email})

            return False

按照注释中的要求,atomic_requests的设置如下:

ATOMIC_REQUESTS = True

Django不允许对象创建对象周围的对象或我在做错事的特定原因是否存在?

Something very strange is happening in my Django app. When I try to run the following code

Model.objects.create(**obj)

before raising an exception OR run the same code inside an except block, the code runs without any issue, but I cannot see the object in the database. Running the Model.objects.all() also doesn't return anything.

This is the complete code:

def has_permission(self, request, view):
        try:
            serializer = VerifyOTPRequestSerializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            otp = request.data.pop("otp")
            flow = request.data.pop("flow")
            primary_phone = request.data.get("contact_detail", {}).get(
                "primary_phone", ""
            )            
            if OTPFailedAttempts.objects.filter(phone=primary_phone).count() > 0:
                raise Exception("otpAttemptError")
            
            return helpers.OTPHelper.verify_otp(primary_phone, otp, flow)
        except Exception as permission_error:
            
            OTPFailedAttempts.objects.create(**{"phone": primary_phone, 'email': email})

            return False

As requested in the comments, the setting for ATOMIC_REQUESTS is as follows:

ATOMIC_REQUESTS = True

Is there a particular reason why Django doesn't allow object creation of objects around exceptions or am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-02-18 05:03:19

问题是您的请求是原子质,因为您将atomic_requests设置为true在您的设置中。

Django REST框架具有一个方法check_permissions,该调用has_permission视图的所有权限类的方法,如果这些返回的任何 false(或一个虚假的值)然后提出异常(参考: github代码)。

由于您的has_permission方法返回false这会引起异常。鉴于您已经设置了atomic_requests = true(django rest框架具有符合此设置的代码),这会导致您的更改回滚。解决方案是删除atomic_requests < / code>,并使用Django提供的Decorator / Context Manager自己处理。

The problem is that your requests are atomic since you have set ATOMIC_REQUESTS to True in your settings.

Django Rest Framework has a method check_permissions which calls the has_permission method of all the permission classes of a view,if any of these return False (or a falsy value) then an exception is raised (Reference: GitHub code).

Since your has_permission method returns False this raises an exception. Given that you have set ATOMIC_REQUESTS = True (Django Rest Framework has code to comply with this setting) this causes your changes to be rolled back. The solution would be to remove ATOMIC_REQUESTS and handle it yourself using the decorator / context manager provided by Django.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文