开发者

Matching 3 out 5 fields - Django

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?


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


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.


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


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:
        ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜