2014-10-26 74 views
1

林導入Django的進出口XLS文件和所有工作正常,現在我需要刪除具有相同字符串的行,我的意思是不要保存在數據庫重複行Django的進出口

id - name 
1 - Jhon 
2 - Jhon 
3 - Peter 

在DB導入行2和3

時到現在爲止,只有插入,我有這個:

class ProyectosResource(resources.ModelResource): 
     #repeated_rows = fields.Field() 

     class Meta: 
      model = Proyectos 

class ProyectosAdmin(ImportExportModelAdmin): 
     resource_class = ProyectosResource 

回答

1

我不知道這是否是這樣做的正確的方式,但我會做到這一點的before_import功能:

class ProyectosResource(resources.ModelResource): 
     #repeated_rows = fields.Field() 

     class Meta: 
      model = Proyectos 

def before_import(self, dataset, dry_run): 
     """ 
     Make standard corrections to the dataset before displaying to user 
     """ 
     list = [] 
     i = 0 
     last = dataset.height - 1 
     while i <= last: 
      #adding each name to a list 
      if ("the name is not in the list (take care about capitalizes letters)"): 
       dataset.rpush((dataset.get_col(0)[0], dataset.get_col(1)[0])) # add this line to the bottom 
       list = list.append(dataset.get_col(1)[0]) # add the name to the list 
       dataset.lpop() # erase it from the top (first line) 
      else : 
       #the name is already in the list 
       dataset.lpop() # simply erase it (first line) from the dataset 
      i = i + 1 

這裏是Tablib的doc來操作數據集!

您可以在before_import函數中進行每個測試,檢查外鍵關係的ID ...