Django DB 设计可快速通配单词

发布于 2024-09-18 11:38:11 字数 994 浏览 15 评论 0原文

我需要快速查找我在 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 的单词。

我不太确定设计和一些机制,所以我想在尝试实现它之前我会在这里征求建议。我不完全确定查询的语法。

所以,我想问题是

  1. 你对设计有什么建议吗?
  2. mapping[2] 有效吗?
  3. 我是否能够将字典传递给过滤器命令,以便我可以拥有可变数量的关键字参数?

谢谢!

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

  1. Do you have any suggestions for the design?
  2. Would mapping[2] work?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-09-25 11:38:11

映射[2]有用吗?

不,不会的。

我是否能够将字典传递给过滤器命令,以便我可以拥有可变数量的关键字参数?

当然。例如:

conditions = dict(word__startswith = 'A', length = 5)
Word.objects.filter(**conditions)

将查找所有以 A 开头且长度为五个字符的 Word 实例。

您对设计有什么建议吗?

在我看来,这就像一个过早优化的例子。我怀疑对于中等数量的数据,您应该能够将合适的数据库功能与 Django 的过滤器结合起来以获得您需要的内容。

例如:

查找第三个位置有 A 的所有长度为 4 的单词。

您可以将 Django 过滤器与(Postgresql 的)strpos

contains, position = 'A', 3
where = ["strpos(word, '%s') = %s" % (contains, position)]
Word.objects.filter(length = 4, word__contains = contains).extra(where = where)

Would mapping[2] work?

No, it wouldn't.

Would I be able to pass in a dictionary to the filter command so that I can have a variable number of keyword arguments?

Certainly. For instance:

conditions = dict(word__startswith = 'A', length = 5)
Word.objects.filter(**conditions)

would find all Word instances starting with A and are five characters long.

Do you have any suggestions for the design?

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:

to find all words of length four that have an A in the third position.

you can combine a Django filter with (Postgresql's) strpos.

contains, position = 'A', 3
where = ["strpos(word, '%s') = %s" % (contains, position)]
Word.objects.filter(length = 4, word__contains = contains).extra(where = where)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文