Q:

Contacts Matching

I have the below Models:
class UserModel(User):
  mobile_number = StringField
  common_name = StringField

class Contacts(models.Model):
  user = ForeignKey(UserModel)
  mobile_number = StringField
  name = StringField

and I created a new UserModel of non repeated mobile number and most commonly used name from Contacts Model.  I implemented it by using:
from collections import Counter

Any professional suggestions ? to get the common name from Contacts Model of each mobile_number of Contacts Model

A:

contact_list = Contacts.objects.all().order_by('name')

name = None

for contact in contact_list:
    if contact.name != name:
        print '-- New Name --'
        print 'name: %s' % contact.name
        name = contact.name
    print 'phone number: %s' % contact.phone 

It's one of millions solutions.. above you have contact_list ordered by names. Counter from collections is also a good approach.. even better then this.

