模型对象没有从内部或在提出异常之前从内部或直接创建的模型对象
我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的请求是原子质,因为您将
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
toTrue
in your settings.Django Rest Framework has a method
check_permissions
which calls thehas_permission
method of all the permission classes of a view,if any of these returnFalse
(or a falsy value) then an exception is raised (Reference: GitHub code).Since your
has_permission
method returnsFalse
this raises an exception. Given that you have setATOMIC_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 removeATOMIC_REQUESTS
and handle it yourself using the decorator / context manager provided by Django.