【发布时间】:2016-12-12 20:47:05
【问题描述】:
我的模型
class Despacho (models.Model):
bus=models.ForeignKey(Bus)
contador = models.IntegerField()
cerrado = models.BooleanField(editable=False)
class Bus(models.Model):
numero_bus=models.CharField(max_length=255,unique=True)
en_ruta = models.BooleanField(editable=False)
我需要一个查询来提取我保存总线的数据,然后我输入一个 公共汽车的号码,我需要知道是否有调度 匹配搜索尝试执行以下查询
我的数据库是postgresql
d = Despacho.objects.raw('''SELECT * FROM operaciones_despacho WHERE operaciones_despacho.bus = '%s' AND operaciones_despacho.cerrado = '%s'
;'''%(bus.numero_bus,False))
错误:operaciones_despacho.bus 列不存在
【问题讨论】:
-
您认为为什么需要执行原始查询?更糟糕的是,为什么你认为你需要错误地执行它?
-
我也想知道你为什么需要原始请求?
-
Ignacio 暗示的是您的代码容易受到 SQL 注入攻击。这是在原始查询上使用 ORM 的重要原因之一:它再次保护您可能导致漏洞的错误。
标签: python django postgresql django-models