简体   繁体   English

如何使用uuid的IN运算符 - Django?

[英]How to use the IN operator with uuid - Django?

How to use the IN operator with uuid - Django? 如何使用uuid的IN运算符 - Django?

Model 模型

class Client(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    nome    = models.CharField(max_length=255)
    email    = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.nome

class Subscription(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    vencimento = models.DateField()
    client = models.OneToOneField(Client, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

View 视图

 clients = Client.objects.filter(nome__icontains=q).values_list('pk', flat=True)
 subscriptions = Subscription.objects.filter(client_id__in=clients)

I tried using this to convert and it still does not work. 我尝试使用它来转换它仍然无法正常工作。

clients = [str(o) for o in clients]

Where am I going wrong? 我哪里错了?

Thank you. 谢谢。

I also tried this 我也尝试过这个

subscriptions = Subscription.objects.filter(client__nome__icontains=q)

Query 询问

SELECT "app_subscription"."id", "app_subscription"."vencimento","app_subscription"."client_id", "app_subscription"."created_at", "app_subscription"."updated_at" FROM "app_subscription" INNER JOIN "cad_client" ON ("app_subscription"."client_id" = "cad_client"."id") WHERE "cad_client"."nome" LIKE %marcelo% ESCAPE '\' 

ERROR 错误

psycopg2.ProgrammingError: operator does not exist: character varying = uuid psycopg2.ProgrammingError:运算符不存在:character varying = uuid

operator does not exist: character varying = uuid LINE 1: ...client" ON ("app_subscriptions"."client_id" = "clien... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 运算符不存在:字符变化= uuid LINE 1:... client“ON(”app_subscriptions“。”client_id“=”clien ... ^提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。

Try to convert it to list: 尝试将其转换为列表:

clients_list = list(Client.objects.filter(nome__icontains=q).values_list('pk', flat=True))
subscriptions = subscription.objects.filter(client__id__in=clients_list)

First thing, your model does not contain the nome filed. 首先,您的模型不包含nome文件。 So, assuming it does, your query returns a queryset. 因此,假设确实如此,您的查询将返回一个查询集。 So, for the IN to work, what you want to pass in is a list. 因此,要使IN工作,您要传递的是列表。

clients = Client.objects.filter(nome__icontains=q).values_list('pk', flat=True)
clients = list(clients)
subscriptions = Subscription.objects.filter(client_id__in=clients)

There's no reason to use two queries, or even a subquery, here. 这里没有理由使用两个查询,甚至是子查询。 A simple related lookup is what you need. 您需要一个简单的相关查找。

subscriptions = Subscription.objects.filter(client__nome__icontains=q)

But note that your actual error, as others have pointed out, is probably because your Client model does not appear to contain a nome field. 但请注意,正如其他人指出的那样,您的实际错误可能是因为您的客户端模型似乎不包含nome字段。

class Subscription(models.Model):

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    vencimento = models.DateField()
    client = models.OneToOneField(Client, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

This column in the database was not of the uuid type. 数据库中的此列不是uuid类型。 He was with Varchar. 他和Varchar在一起。

So it was not possible to compare. 所以无法进行比较。

Just change the type of the Varchar column to uuid in the database. 只需在数据库中将Varchar列的类型更改为uuid即可。

Thank you all for your attention. 谢谢大家的关注。

client = models.OneToOneField(Client, on_delete=models.CASCADE)

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

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