Django:过滤器返回空结果

时间:2019-04-30 08:58:49

标签: python django postgresql django-queryset

我的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

从此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}>"

我想从ID中检索workspace_role_name

但是

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

是一个空查询集<QuerySet []>,但是当我运行

WorkspaceRole.objects.all()

我得到正确的输出:<QuerySet [<WorkspaceRole: Admin>, <WorkspaceRole: Owner>, <WorkspaceRole: User>]>

我的过滤器出了什么问题?

2 个答案:

答案 0 :(得分:1)

升级您的searching ...

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

答案 1 :(得分:0)

与同事进行一些修补后,解决方案是使用:

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