Commits

hideki nara  committed 9993043

exclude fields

  • Participants
  • Parent commits fa996f7

Comments (0)

Files changed (4)

File csvutils/models.py

 
 class CsvModelManager:
 
-    def from_dictrow(self,dictrow,keys=['id']):
+    def from_dictrow(self,dictrow,keys=['id'],excludes=[],save=True):
         ''' 
         .. todo::
             
             - data sanitizing
             - relation fields
         '''
-        key_vals = dict([(k,v) for k,v in dictrow.items() if k in keys ] )
+        key_vals = dict([(k,v) for k,v in dictrow.items() 
+            if k in keys and k not in excludes ] )
         try:
             rec = self.get(**key_vals)
             try:
             return None,e
             
         try:            
-            rec.save()
+            if save:
+                rec.save()
             return rec ,None
         except Exception,e:
             return rec, e
         
-    def csv_load(self,stream,header=True,encoding=None,keys=['id'],dialect='excel'):
+    def csv_load(self,stream,header=True,encoding=None,keys=['id'],dialect='excel',save=True,excludes=[]):
 
         if header==False:
-            fieldnames = [ f.name for f in self.model._meta.fields if not isinstance(f,AutoField) ]
+            fieldnames = [ f.name for f in self.model._meta.fields 
+            if not isinstance(f,AutoField) and f.name not in excludes]
         else:
             fieldnames=None
 
         for index,row in UnicodeReader.csv_enumerator(stream,fieldnames,dialect):
-            rec,e = self.from_dictrow(row,keys)
+            rec,e = self.from_dictrow(row,keys=keys,excludes=excludes,save=save)
             yield index,rec,e 
 
     def csv_dump(self,queryset=None,fields=None, exclude=[], display=True, *args, **kwargs):

File test/app/sample/fixtures/building.csv

+0600032,test,1-1-1

File test/app/sample/models.py

         except:
             return False
 
-    @property
-    def address(self):
-        town = self.town.replace(u'区以下に掲載がない場合','')
+class Building(models.Model):
+    jpaddr= models.ForeignKey(JpAddr,null=True,blank=True,default=None)
+    zip  = models.CharField(max_length=7)
+    name = models.CharField(max_length=100)
+    street = models.CharField(max_length=100)
+
+    objects = JpAddrManager() 

File test/app/sample/tests.py

                             keys=['zip']):
             print i,",",addr.id,addr.zip,",",e
         self.assertEqual(i+1,JpAddr.objects.count() )
+
+        from models import Building
+        for i, addr , e in Building.objects.csv_load( 
+                            open(self.fixture_path('fixtures','building.csv')),
+                            header=False,
+                            save=False, 
+                            excludes=['jpaddr'],
+                            keys=['zip','name','street']):
+                            
+            if addr:
+                try:
+                    addr.jpaddr = JpAddr.objects.get(zip=addr.zip)
+                    addr.save()
+                except Exception,e:
+                    print e
+        self.assertEqual(i+1,Building.objects.count() )
+