简体   繁体   English

Django:过滤器返回空结果

[英]Django: filter returns empty result

I have the following table in my PostGreSQL database : 我的PostGreSQL数据库中有下表:

          workspace_role_id           | workspace_role_name 
--------------------------------------+---------------------
 3f76103f-732a-435a-a88f-737f4a6f1b87 | Owner
 c73b7c35-237e-4e13-8269-b259c2858b71 | Admin
 a61890fc-1c29-4817-8687-30786a5db17a | User

built from this Django model: 从以下Django模型构建:

class WorkspaceRole(models.Model):
    class Meta:
        ordering = ("workspace_role_name",)

    workspace_role_id = models.UUIDField(primary_key=True, default=uuid.uuid4, blank=True, editable=False)
    workspace_role_name = models.CharField(max_length=64, unique=True)

    def __repr__(self):
        return f"<{self.__class__.__name__}: {self.workspace_role_name}>"

And I want to retrieve the workspace_role_name from the ID. 我想从ID中检索workspace_role_name

However, the result of 但是,结果

WorkspaceRole.objects.filter(workspace_role_id="a61890fc-1c29-4817-8687-30786a5db17a")

is an empty queryset <QuerySet []> , but when I run 是一个空的查询集<QuerySet []> ,但是当我运行时

WorkspaceRole.objects.all()

I get the correct output: <QuerySet [<WorkspaceRole: Admin>, <WorkspaceRole: Owner>, <WorkspaceRole: User>]> 我得到正确的输出: <QuerySet [<WorkspaceRole: Admin>, <WorkspaceRole: Owner>, <WorkspaceRole: User>]>

What am I doing wrong with my filter? 我的过滤器出了什么问题?

Upgrade your searching ... 升级您的搜索 ...

import uuid
id = uuid.UUID('a61890fc-1c29-4817-8687-30786a5db17a')
WorkspaceRole.objects.filter(workspace_role_id=id)

After some tinkering with a colleague, the solution was to use: 与同事进行一些修补后,解决方案是使用:

with in_database(my_db, write=True):
    id = uuid.UUID('a61890fc-1c29-4817-8687-30786a5db17a')
    WorkspaceRole.objects.filter(workspace_role_id=id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM