从Django Auth用户模型中查找Common first_name

时间:2014-02-28 14:05:22

标签: python django mongodb django-models

我需要找出邮件info@something.com的常见first_name(不区分大小写)

我正在使用MongoDb and MongoEngine。查询应该如何处于正常的ORM

用户列表:

first_name = 'Anish Menon'
email = 'info@something.com'



first_name = 'Hari Dev'
email = 'info@something.com'


first_name = 'anish MenoN'
email = 'info@something.com'

初​​始:

Users = User.objects.filter(email='info@something.com')

for user in Users:
  --------------

1 个答案:

答案 0 :(得分:2)

怎么样:

import operator

然后在循环中,获取小写的first_name列表:

list = []
list.append(user.first_name.lower()) # to have lowercase strings

然后,计算first_name的迭代:

dict = {}
for el in list:
    if el in dict:
        dict[el] += 1
    else:
        dict[el] = 1

max(dict.iteritems(), key=operator.itemgetter(1))[0] # This is your result

享受:)