存储此浏览器版本的好方法 - Django/Postgresql
我有这个数据:
火狐3.6
有 3 个项目
- 名称
- 最大版本
- 最小版本
我以这种方式存储它:
class MyModel(models.Model):
browser_name = models.CharField(...)
browser_max_version = models.IntegerField(...)
browser_min_version = models.IntegerField(...)
或替代方案
class Browser(models.Model):
name = models.CharField(...)
max_version = models.IntegerField(...)
min_version = models.IntegerField(...)
class MyModel(models.Model):
browser = models.ForeignKey(Browser)
是否有任何聪明的方法可以将值存储在 1 个字段中并使其同时可解析?
我知道这可能听起来很奇怪,但我想知道除了构建 100 万个模型来表示数据之外,是否还有其他选择。
有什么想法吗? :)
I have this data:
Firefox 3.6
There are 3 items
- name
- max version
- min version
I am storing it this way:
class MyModel(models.Model):
browser_name = models.CharField(...)
browser_max_version = models.IntegerField(...)
browser_min_version = models.IntegerField(...)
or alternative
class Browser(models.Model):
name = models.CharField(...)
max_version = models.IntegerField(...)
min_version = models.IntegerField(...)
class MyModel(models.Model):
browser = models.ForeignKey(Browser)
Is there any clever way to store the value in 1 field and making it parsable at the same time?
I know this might sound weird, but I wonder if there are any alternative to building 1 million models to represent data.
Any ideas? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使其可解析,但可能不可索引。例如,您可以将值连接在一起,并用分号(或其他字符)分隔,然后简单地拆分字符串以获取值。 “Firefox 3.6”将变为“Firefox;3;6”。虽然这更容易解析,但与原始格式相比并没有提供太多优势。
这种方法的一个重要警告是该列不能以非常精细的方式进行索引。例如,您无法要求所有版本的 Firefox。 PostgreSQL 允许一些非常高级的索引,我相信,这将允许您创建所需的索引,但我不知道您可以通过 Django 的 ORM 访问索引的任何方式。
You could make it parseable, but probably not indexable. For example, you could concatenate the values together separated by semicolons (or some other character), then simply split the string to get the values back. "Firefox 3.6" would become "Firefox;3;6". While this is somewhat easier to parse, it doesn't provide much of an advantage over the original formatting.
The big caveat with this approach is that the column wouldn't be indexable in a very granular way. For example, you couldn't ask for all versions of Firefox. PostgreSQL allows for some very advanced indexing which, I believe, would allow you to create the required indexes, but I don't know of any way you could access the indexes via Django's ORM.
第二个示例中
MyModel
的用途是什么?您只需要一张表Browser
。到底为什么需要“数百万”个模型?或者你在谈论表中的行?很好
What is the purpose of
MyModel
in the second example? The one tableBrowser
is all you need. Why on earth would you need 'millions' of models? Or are you talking about rows in a table?is fine