/django-xlspopulator

Populate your Django model from .xls file

Primary LanguagePythonMIT LicenseMIT

django_xlspopulator

Easy to use .xls Django model populator.

pip install django-xlspopulator

How to:

  1. In order for this to work you need exactly match your model's field names and xls's first row values(column names), like this:
    class Test(models.Model):
      one = models.CharField(max_length=150)
      two = models.CharField(max_length=150)
      three = models.CharField(max_length=150)
      four = models.CharField(max_length=150)
  2. Then you need to create populate.py file at the one level with your manage.py
  3. Put following code there:
    # populate.py
    import os
    import django
    from django_xlspopulator.populator import Populator
    os.environ.setdefault('DJANGO_SETTINGS_MODULE','YOURPOJECT.settings')
    django.setup()
    from YOURAPP.models import Test
    
    pop = Populator('C:/Users/Guido/Desktop/testfile.xls', Test)
    pop.populate()
    • Populator object takes three arguments:
      • path to the .xls file in form of a string
      • Django model object
      • Sheet number, int(0 by defaulft, specify it if you want to use other sheet)
  4. Run populate.py from the terminal and wait. For my old machine with SQLite it took 7 mins to populate model from file that had 4 columns and 10000 rows. It works MUCH faster with MySQL or PostgreSQL.