我的模型
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))
error : column operaciones_despacho.bus does not exist
首先在Django中,我们只在编写ORM查询特别困难的情况下使用原始sql。在本例中,编写ORM查询要比原始查询简单得多,而且要短得多。你知道吗
在需要进行原始查询的少数情况下,请注意使用params参数来进行原始查询,而不是使用字符串格式。编写原始查询的正确方法是
但我再次强调,您不应该在这里使用原始查询,因为它是一个简单的ORM查询。你知道吗
相关问题 更多 >
编程相关推荐