匹配 5 个字段中的 3 个 - Django

发布于 2024-09-04 09:27:45 字数 348 浏览 6 评论 0原文

我觉得这有点棘手!也许有人可以帮助我解决这个问题,

我有以下模型:

class Unicorn(models.Model):

  horn_length = models.IntegerField()
  skin_color = models.CharField()
  average_speed = models.IntegerField()
  magical = models.BooleanField()
  affinity = models.CharField()

我想搜索所有具有至少 3 个共同字段的类似独角兽。


是不是太刁钻了?或者说是可行的吗?

I'm finding this a bit tricky! Maybe someone can help me on this one

I have the following model:

class Unicorn(models.Model):

  horn_length = models.IntegerField()
  skin_color = models.CharField()
  average_speed = models.IntegerField()
  magical = models.BooleanField()
  affinity = models.CharField()

I would like to search for all similar unicorns having at least 3 fields in common.


Is it too tricky? Or is it doable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

当梦初醒 2024-09-11 09:27:45

您应该使用 Q 对象。粗略的例子是:

from django.db.models import Q
from itertools import combinations
# this -- the unicorn to be matched with
attr = ['horn_length', 'skin_color', 'average_speed', 'magical', 'affinity']
q = None
for c in combinations(attrs, 3):
    q_ = Q(**{c[0]: getattr(this, c[0])}) & Q(**{c[1]: getattr(this, c[1])}) & Q(**{c[2]: getattr(this, c[2])})
    if q is None:
        q = q_
    else:
        q = q | q_
Unicorn.objects.get(q)           

虽然没有测试过

You should use Q objects. The rough example is:

from django.db.models import Q
from itertools import combinations
# this -- the unicorn to be matched with
attr = ['horn_length', 'skin_color', 'average_speed', 'magical', 'affinity']
q = None
for c in combinations(attrs, 3):
    q_ = Q(**{c[0]: getattr(this, c[0])}) & Q(**{c[1]: getattr(this, c[1])}) & Q(**{c[2]: getattr(this, c[2])})
    if q is None:
        q = q_
    else:
        q = q | q_
Unicorn.objects.get(q)           

not tested, though

我是男神闪亮亮 2024-09-11 09:27:45

它必须在 HAVING 子句中完成:

SELECT ... HAVING (IF(a.horn_length=b.horn_length, 1, 0) + ...) >= 3

在 Django ORM 中无法表达 HAVING,因此您需要放到 原始 SQL 以便执行它。

It has to be done in the HAVING clause:

SELECT ... HAVING (IF(a.horn_length=b.horn_length, 1, 0) + ...) >= 3

There's no way to express HAVING in the Django ORM so you'll need to drop to raw SQL in order to perform it.

幸福%小乖 2024-09-11 09:27:45

如果我理解正确的话,这应该涵盖您的问题:

from django.db import models

Unicorn.objects.filter(models.Q(skin_color = 'white') | models.Q(magical = True))

这将过滤所有肤色为白色或有一些共同魔法的东西的独角兽。有关 Q 对象的更多信息,请参见此处 http ://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

This should cover your question, if I understood it right:

from django.db import models

Unicorn.objects.filter(models.Q(skin_color = 'white') | models.Q(magical = True))

This would filter all unicorns that have skin color white or have some magical stuff in common. More about the Q objects here http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

不知所踪 2024-09-11 09:27:45

我从未使用过 Django,而且我在 Python 方面相当新手,但也许你可以做这样的事情:

创建一个方法来比较 Unicorn 类的两个实例。

def similarity(self, another)
    sim = 0
    if (self.horn_length==another.horn_length):
        sim+=1
    if (self.skin_color==another.skin_color):
        sim+=1
    if (self.average_speed==another.average_speed):
        sim+=1
    if (self.magical==another.magical):
        sim+=1
    if (self.affinity==another.affinity):
        sim+=1
    return sim

然后你可以用类似的东西进行测试:

myUnicorn
for x in unicornsList:
    if myUnicorn.similarity(x) >=3:
        ...

I have never used Django and i'm rather novice in Python but perhaps you can do something like this:

make a method that compares two instances of the class Unicorn.

def similarity(self, another)
    sim = 0
    if (self.horn_length==another.horn_length):
        sim+=1
    if (self.skin_color==another.skin_color):
        sim+=1
    if (self.average_speed==another.average_speed):
        sim+=1
    if (self.magical==another.magical):
        sim+=1
    if (self.affinity==another.affinity):
        sim+=1
    return sim

Then you can test with something like:

myUnicorn
for x in unicornsList:
    if myUnicorn.similarity(x) >=3:
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文