· 8 years ago · Mar 10, 2018, 09:44 PM
1class SecurityPrice (models.Model):
2 security = models.ForeignKey(Security, on_delete = models.CASCADE)
3 created = models.DateTimeField()
4 price = models.FloatField()
5
6 class Meta:
7 ordering = ('-created',)
8
9 def __unicode__(self):
10 return u'%s - %s - %s' % (self.created, self.security.ticker, self.price)
11
12CREATE TABLE IF NOT EXISTS "restful_api_securityprice"
13("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
14"created" datetime NOT NULL,
15"price" real NOT NULL,
16"security_id" integer NOT NULL REFERENCES "restful_api_security" ("id"));
17
18SELECT MAX(created), security_id, price as created
19FROM restful_api_securityprice GROUP BY security_id;
20
21sqlite> SELECT * FROM restful_api_securityprice;
221|2018-01-07 23:13:02|920.0|1
232|2018-01-07 23:13:43|137.12|2
243|2018-01-07 23:13:58|147.3|3
254|2018-01-09 00:46:29|920.0|1
265|2018-01-09 00:47:27|137.12|2
276|2018-01-09 00:48:08|147.3|3
28
29sqlite> SELECT MAX(created), security_id, price as created FROM restful_api_securityprice GROUP BY security_id;
302018-01-09 00:46:29|1|920.0
312018-01-09 00:47:27|2|137.12
322018-01-09 00:48:08|3|147.3