如何使用带uuid-Django的IN运算符?在
型号
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)
视图
^{pr2}$我试着用这个来转换,但还是没用。在
clients = [str(o) for o in clients]
我哪里出错了?在
谢谢。在
我也试过这个
subscriptions = Subscription.objects.filter(client__nome__icontains=q)
查询
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 '\'
错误
psycopg2.ProgrammingError: operator does not exist: 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.
首先,您的模型不包含
nome
字段。所以,假设它是这样的,那么您的查询将返回一个queryset。因此,为了使IN
正常工作,您需要传递的是一个列表。在尝试将其转换为列表:
这里没有理由使用两个查询,甚至一个子查询。一个简单的相关查找就是您需要的。在
但请注意,正如其他人所指出的,您的实际错误可能是因为您的客户机模型似乎不包含
nome
字段。在相关问题 更多 >
编程相关推荐