Skip to main content

Populate your Django model from .xls file

Project description

django_xlspopulator

Easy to use .xls Django model populator.

How to:

  1. First you need to install latest version of xlrd: pip install xlrd==1.1.0
  2. 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)
    
  3. Then you need to create populate.py file at the one level with your manage.py
  4. 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)
  5. Run populate.py from the terminal and wait. For my old machine with SQLite it took 5 mins to populate model from file that had 4 columns and 10000 rows.

Project details


Release history Release notifications | RSS feed

This version

1.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_xlspopulator-1.1.tar.gz (2.3 kB view hashes)

Uploaded Source

Built Distribution

django_xlspopulator-1.1-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page