1

I am building an app that look for each phone number in the database. If there is any duplicate, I want to grab the first phone number found as the main record for that phone number, then for the duplicate information(name, location), get each one of those fields, and add it to the main record phone number fields (name, location), separated by a semi colon.

The outcome would look like this after checking the duplicate information of the main phone number record found:

Name                      Location               Phone number
Helene,Sandra             New Yok, Boston        000-000

Please find my model below:

class Document(models.Model):
    name = models.CharField(null=True, max_length=254, blank=True)
    location = models.CharField(null=True, max_length=254, blank=True)
    phone_number = models.CharField(null=True, max_length=254, blank=True)

I am a bit lost on to achieve the above. Any help would be much appreciated.

Below is what I have tried so far:(not working)

 from django.shortcuts import render
    from .models import Document

    def index(request):
        search_number = list(Document.objects.order_by('-created').values("phone_number").distinct().order_by()) # Dictionary list of all numbers sorted by creation data without duplicate

        for x in search_number:
            try:
                look_up = Document.objects.values("phone_number")
                list_in_dba = look_up.phone_number
                x in list_in_dba['phone_number']
                print("Yes")
            except:
                print("No")

        return render(request, 'snippets/index.html')   
Curtis Banks
  • 342
  • 4
  • 20
  • What have you tried so far? – ferrix Feb 21 '19 at 23:20
  • @ferrix I have created a dictionary queryset of phone numbers. I am trying to find the right second queryset to check if a phone number in the database belong to that dictionary to apply a logic to it. Thinking of using map function to check and apply the same function to all elements of the dictionary. I am failing to create the second queryset. Am i doing the right thing here? – Curtis Banks Feb 22 '19 at 00:09
  • 1
    can you add what you have tried for the queryset filtering to get the records as per your requirements? – Angela Feb 22 '19 at 01:21
  • @Angela I have edited the post with the code above of what i have tried so far. I am using print to monitor the behavior of what is returned. – Curtis Banks Feb 22 '19 at 01:49
  • @CurtisBanks Have you checked my answer? – Angela Feb 24 '19 at 22:43
  • @Angela I did thank you so much. Please take a look at my edit of your codes below, I made a small change. The last thing I am trying to figure out is how to insert the value of all the names and phone numbers associates with a phone number to 'name' cell and 'location' in the database. If I had Eric and Thomas sharing the same phone number, name in the database would be "Eric, Thomas". What I tried below does not allow me save the first name. – Curtis Banks Feb 25 '19 at 02:29
  • @CurtisBanks Please include any concerns for code as comments to the reply or edits to your actual question. I am editing the answer to accomodate your change. – Angela Feb 25 '19 at 02:34
  • @Angela Thank you so much!!! You just made my day! – Curtis Banks Feb 25 '19 at 02:52

1 Answers1

1

I would start with something like this.

## this will get you all document records that have a duplicate phone-number 
## and also group them by phone-number.
duplicate_phone_numbers = Document.objects.values('phone_number').\
    annotate(total_items=Count('phone_number')).order_by('-total_items').filter(total_items__gt=1)

for entry in duplicate_phone_numbers:
    records = Document.objects.filter(phone_number=entry.get('phone_number')
    ## unsure whether you want to just output the info here or 
    ## update the actual record
    all_names = ''
    all_locations = ''
    for x in records:
        all_names += x.name + ";"
        all_locations += x.location + ";"
    print all_names, all_locations, entry.get('phone_number')
    # to update the actual record
    record = records[0]
    record.name = all_names
    record.location = all_locations
    record.save()
Angela
  • 1,671
  • 3
  • 19
  • 29