2016-07-01 4 views
1

私は、Djangoへのデータの一括アップロードを可能にするモジュールを検索/作成しようとしています。私は特定のフェスティバルに多くのアーティストを関連づけようとしていますが、今まで私が見つけたすべてのモジュールは1:1のアップロードのみをサポートしています。 https://github.com/edcrewe/django-csvimportCSVをDjangoに一括アップロードすることはできますか?

+0

はいできます。あなたはそのためのカスタムコードを書く必要があります。私は電子商取引プロジェクトのためにそれをしました。 – xyres

+0

がそれを手に入れました。あなたがインスピレーションのためにそれをした方法をあなたが共有することができたらどんなチャンスですか? –

+0

以下に例を掲載します。 – xyres

答えて

1

あなたがしたいものの例です。大きすぎるので元のコードを削除しました。しかし、これはまだあなたに良い出発点を与えるでしょう。

実際には、クライアントがアップロードしたCSVファイルは、この例で示したように単純ではありませんでした。いくつかの行には空白があり、いくつかの行にはファイル名、店舗名などの不要な情報が含まれていたので、そのデータをフィルタリングするためにいくつかのメソッドを追加しなければなりませんでした。しかし、私はそれを下にしていません。私は可能な限り最小限に抑えてきました。

from django.db import models 
import csv 


class Category(models.Model): 
    name = models.CharField(max_length=10) 


class Product(models.Model): 
    category = models.ForeignKey(Category) 
    uid = models.CharField(max_length=4) 
    name = models.CharField(max_length=10) 
    price = models.IntegerField() 
    qty = models.IntegerField() # quantity 


class CSVFile(models.Model): 
    """ 
    This model allows for uploading a CSV file and then 
    updates data in Category and Product models 
    """ 
    csv_file = models.FileField(upload_to='csvfiles') 
    # feel free to add other fields like upload date, etc. 

    def save(self, *args, **kwargs): 
     """ 
     This is where you analyze the CSV file and update 
     Category and Product models' data 
     """ 
     super(CSVFile, self).save(*args, **kwargs) 
     self.csv_file.open(mode='rb') 
     f = csv.reader(self.csv_file) 
     for row in f: 
      # currently the row is a list of all fields in CSV file 
      # change it to a dict for ease 
      row_dict = self.row_to_dict(row) # this method is defined below 
      # check if product exists in db 
      product = self.product_is_in_db(row_dict['uid']) # this method is defined below 
      if product: 
       # product is in db 
       # update fields values 
       self.update_product(product, row_dict) # this method is defined below 
      else: 
       # product is not in db 
       # create this product 
       self.create_product(row_dict) # this method is defined below 

     self.csv_file.close() 


    def row_to_dict(self, row): 
     """Returns the given row in a dict format""" 
     # Here's how the row list looks like: 
     # ['category', 'product name', 'product uid' 'price', 'qty'] 
     return {'category': row[0], 'name': row[1], 
      'uid': row[2], 'price': row[3], 'qty': row[4] 
      } 

    def product_is_in_db(self, uid): 
     """Check the product is in db. 
     If yes, return the product, else return None 
     """ 
     try: 
      return Product.objects.get(uid=uid) 
     except Product.DoesNotExist: 
      return None 

    def update_product(self, product, row_dict): 
     """Update the given product with new data in row_dict""" 
     product.price = row_dict['price'] 
     product.qty = row_dict['qty'] 
     product.save() 

    def create_product(self, row_dict): 
     # First see, if category exists 
     # If not, create a new category 
     try: 
      category = Category.objects.get(row_dict['category']) 
     except Category.DoesNotExist: 
      category = Category.objects.create(name=row_dict['category']) 

     # Now, create the product 
     Product.objects.create(
      name=row_dict['name'], 
      uid=row_dict['uid'], 
      price=row_dict['price'], 
      qty=row_dict['qty'], 
      category=category, 
     ) 
+1

私は['csv.DictReader'](https://docs.python.org/2/library/csv.html#csv.DictReader)を使って反復と解析のコードを減らすのが好きです。 – tutuDajuju

+1

@tutuDajujuええ、私はそれが良い方法だと同意します。しかし、このコードを元にした元のアプリケーションでは、CSVファイルの行に一貫性がありませんでした。一部の行に不要なデータが含まれていました。だから私は手動でdictに行を変換しなければならなかった。しかし、はい、この例のために私はちょうど 'csv.DictReader'を使っていました。 – xyres

関連する問題