ThoughtWin Blog

On product management, engineering, design, culture and many more..

django-import-export

  • hire python developers
  • Ishwar Mandloi
  • Nov 11

django web development with python

django-import-export

Introduction:-

While the name hints, that is a library to handle importing and exporting data. The Django-import-export library supports multiple forms, including CSV, JSON, YAML, and all other formats established by tabling. It also has a Django admin sequence, which is especially convenient to use.


Installation:-


Pip is the way to go:


# pip install django-import-export


Update your settings.py:

There is more an optional arrangement that I usually add:


# IMPORT_EXPORT_USE_TRANSACTIONS = True


The default value is False. It decides if the library will use database actions on data import, just to be on the secure side.


Sources:-



The Django-import-export building work with the idea of Resource, which is a class description very similar to how Django handles model forms and admin classes.


In the documentation, the organizers suggest putting the code related to the resources inside the admin.py file. But if the implementation is not linked to the Django admin, I usually prefer to build a new module named resources.py inside the app folder.




models.py

resources.py


from import_export import resources

from .models import Lic,Lead,Mutual_Fund


class LicResource(resources.ModelResource):

  class Meta:

    model = Lic

     

     

     

     

Here is the most simplistic explanation. You can pass several arrangements to the Meta class like fields, exclude, etc. See the documentation for more details.




Exporting Data Views Example:-



Exporting to CSV,JSON,Excel view:

Importing Data:-


View the data new_Lead.csv:-

Each id must be already because it is the primary key. But it will be created though, so we don’t need to blueprint the value.


import_lead.html

Each id must be already because it is the primary key. But it will be created though, so we don’t need to blueprint the value.


import_lead.html

Django Admin Site:-

Simply use ImportExportModelAdmin rather of ModelAdmin. admin.py

Also, you will already mark the Import and Export buttons.


Conclusion:-


This is a vast Django library. There is much extra you can do with it. Absolutely worth owning a look at the API reference.


Created:11/11/21

Tags:

Hello

round pink area

Award and Certifications

We have worked with some globally recognised organisations and produced perfectly

sized web and mobile apps for them.