Django DB 设计可快速通配单词
我需要快速查找我在 Django 中编写的 Web 应用程序的单词。我正在考虑将单词的每个字符放入其自己的整数字段中,并按位置索引。
class Word(models.Model):
word = models.CharField(max_length=5)
length = models.IntegerField()
c0 = models.IntegerField(blank=True, null=True)
c1 = models.IntegerField(blank=True, null=True)
c2 = models.IntegerField(blank=True, null=True)
c3 = models.IntegerField(blank=True, null=True)
c4 = models.IntegerField(blank=True, null=True)
mapping = [c0, c1, c2, c3, c4,]
def save(self):
self.length = len(self.word)
for (idx, char) in enumerate(self.word):
self.mapping[idx] = ord(char)
models.Model.save(self)
然后我可以进行类似 Word.objects.filter(length=4, mapping[2]=ord('A'))
的查询来查找第三个位置有 A 的所有长度为 4 的单词。
我不太确定设计和一些机制,所以我想在尝试实现它之前我会在这里征求建议。我不完全确定查询的语法。
所以,我想问题是
- 你对设计有什么建议吗?
mapping[2]
有效吗?- 我是否能够将字典传递给过滤器命令,以便我可以拥有可变数量的关键字参数?
谢谢!
I need to quickly look up words for a web application that I am writing in Django. I was thinking of putting each character of the word in an integerfield of its own, indexed by position.
class Word(models.Model):
word = models.CharField(max_length=5)
length = models.IntegerField()
c0 = models.IntegerField(blank=True, null=True)
c1 = models.IntegerField(blank=True, null=True)
c2 = models.IntegerField(blank=True, null=True)
c3 = models.IntegerField(blank=True, null=True)
c4 = models.IntegerField(blank=True, null=True)
mapping = [c0, c1, c2, c3, c4,]
def save(self):
self.length = len(self.word)
for (idx, char) in enumerate(self.word):
self.mapping[idx] = ord(char)
models.Model.save(self)
Then I could make queries like Word.objects.filter(length=4, mapping[2]=ord('A'))
to find all words of length four that have an A in the third position.
I'm not really sure about the design and some of the mechanics so I thought I would ask for suggestions here before I went and tried to implement it. I'm not entirely sure about the syntax for making queries.
So, I guess the questions would be
- Do you have any suggestions for the design?
- Would
mapping[2]
work? - Would I be able to pass in a dictionary to the filter command so that I can have a variable number of keyword arguments?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,不会的。
当然。例如:
将查找所有以
A
开头且长度为五个字符的Word
实例。在我看来,这就像一个过早优化的例子。我怀疑对于中等数量的数据,您应该能够将合适的数据库功能与 Django 的过滤器结合起来以获得您需要的内容。
例如:
您可以将 Django 过滤器与(Postgresql 的)
strpos
。
No, it wouldn't.
Certainly. For instance:
would find all
Word
instances starting withA
and are five characters long.This feels to me is like a case of premature optimization. I suspect that for moderate volumes of data you should be able to combine a suitable database function with Django's filter to get what you need.
For example:
you can combine a Django filter with (Postgresql's)
strpos
.