Django Rest Framework read file upload -


i need read contents of csv file , save model.

# model class fileupload(models.model):     datafile = models.filefield(upload_to=file_path_name)   # signal read fileupload instance @receiver(post_save, sender=fileupload) def fileupload_post_save(sender, instance, *args, **kwargs):     open(instance.datafile, 'rb') f:         reader = csv.dictreader(f, delimiter='\t')         row in reader:             print row 

the serializer file.

# serializer class fileuploadserializer(serializers.modelserializer):      class meta:     model = fileupload 

when upload file, appears error.

got `typeerror` when calling `fileupload.objects.create()`.  may because have writable field on serializer class not valid argument `fileupload.objects.create()`. may need make field read-only, or override fileuploadserializer.create() method handle correctly. original exception text was: coercing unicode: need string or buffer, fieldfile found. 

the open() method should not open instance of filefield file?

does have better idea parsing file? upload file , read or read before saving? thanks!!

this solution. it's necessary pass request directly dictreader:

if serializer.is_valid():     data = self.request.data.get('datafile')     reader = csv.dictreader(data, delimiter='\t')     row in reader:         print row['customer'] 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -