'Django: Removing duplicated data in model

I am getting a data directly from an API and store it in list in dictionary format and then loads the data into the database , but whenever i open that page the data gets duplicated even tho i am clearing the list in the end. I am getting the data in inter_list that is in JSON format, then i convert it to dictionary and find the desired key value pairs using for and if and then i put them in a list. That i then use to populate database.


inter_list = response.text
   inter_list = parse_conf(inter_list)
   inter_list = inter_list['dataplane']
   count = 0
   for a, b in inter_list.items():
       for c, d in b['vif'].items():
           for ifkey, ifvalue in d.items():
               if 'description' in ifkey:
                   description = d[ifkey]
               if 'address' in ifkey:
                   address = d[ifkey]
               # else:
               #     address = '-'
               if 'disable' in ifkey:
                   disable = d[ifkey]
               else:
                   disable = '-'
               if 'firewall' in ifkey:
                   firewall_in = d[ifkey]['in']
                   firewall_out = d[ifkey]['out']
           sl.append({
               'vif': c,
               'description': description,
               'address': address,
               'firewall_out': firewall_out,
               'firewall_in': firewall_in,
               # 'color': color,

           })
   for a in sl:
           data = customer(vif=str(a['vif']), customer_name=a['description'], ips=str(a['address']),
                    firewall_in=str(a['firewall_in']),
                    firewall_out=str(a['firewall_out']))  # , current_color=str(a['color']))
           data.save()


Solution 1:[1]

You probably need to take a step back and see if you really want to save all that data in the DB on every iteration? Maybe there are a few values which needs to be saved for your processing and rest don't. What are you trying to achieve here, what is your logic?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NBhatti