我有一个简单的Django模型
class Server(models.Model):
name = models.CharField(max_length=120)
class ServerPropertie(models.Model):
name = models.CharField(max_length=120)
value = models.CharField(max_length=120)
timestamp = models.DateTimeField()
server = models.ForeignKey(Server)
我想将get_properties方法添加到服务器模型中,它将返回当前服务器的所有最后属性。我的意思是,它应该返回当前服务器的所有属性名的名称和值,并且每个uniq属性名都应该有一个值,该行有一个最大时间戳。在
我可以用原始硬编码的原始SQL(我使用postgres):
^{pr2}$或者在python中,但是我相信存在python解决方案。你能帮帮我吗。在
如果使用的是PostgreSQL,通常的语法是:
来自PostgreSQL documentation:
你可以在sql fiddle demo中看到并尝试它。在
可以将此语法从django documentation转换为django:
因此,在django,它将类似于:
^{pr2}$相关问题 更多 >
编程相关推荐