1
所以我有一個模型Education
,它附加到UserProfile
。這種模式的工作原理如下:User
已經去過不同地區的幾所學校。Django和Python的基本匹配
我想要做的是根據用戶去哪裏做一個「分數」。基本上,如果他們去同一所學校,他們得到10分,同樣的城市,5分,相同的狀態,2分,等等。
我已經做了一些功能來嘗試這個,但我失敗了。有什麼建議?
info = {}
def edu_info(user1):
user_1_cities = []
user_1_schools = []
user_1_state = []
first_one = Education.objects.filter(owner=user1)
for i in first_one:
user_1_cities.append(str(i.city))
user_1_schools.append(str(i.school))
user_1_state.append(str(i.state))
info[str(i.owner.username)] = {}
info[str(i.owner.username)]['cities'] = user_1_cities
info[str(i.owner.username)]['schools'] = user_1_schools
info[str(i.owner.username)]['state'] = user_1_state
return info
def check_match(user1, user2):
match_score = {}
first_info = edu_info(user1)
dict = edu_info(user2)
for item in dict:
cities = dict[item]['cities']
#user2 = item
#print cities
for city in cities:
if city in first_info['jmitchel3']['cities']:
match_score['user'] = 'jmitchel3'
match_score['user2'] = str(user2.user.username)
match_score['city'] = city
print "here! " + str(city)
else:
print "not here! " + str(city)
return match_score
check_match(j,t)
好傢伙!棒極了。謝謝你的幫助! (也很高興知道我是在正確的軌道上!) – jmitchel3 2013-02-11 23:41:07
Wilduck,有沒有一種方法來遍歷django模型中的所有字段並分配給字典?因此,像'user_1_schools.append(str(i.major))'這樣的東西會被添加到組合中,而不會對其進行硬編碼?或者這樣做沒有意義,因爲我也必須指定點值? – jmitchel3 2013-02-11 23:46:37
@ jmitchel3我對Django並不是很熟悉,但是您可以在QuerySet的'values'方法上做些事情,請參閱:https://docs.djangoproject.com/en/dev/ref/models/querysets /#值。但是,在這種情況下,如果你沒有太多的領域,手工操作可能會更加清晰。 – Wilduck 2013-02-12 00:08:00