parser_app has been created intending to import Excel file data to django models in a convenient manner. This is a rest_framework based packege and is usuable in many projects.
This package is only compatible with PostgreSQL (tested with PostgreSQL version 13 and 13.3)
Feel free to contribute in the parser_app
. Your contributions are always appreciated!
- Install django-parser-app
pip install django-parser-app
-
Add
parser_app
and it's dependencies to yourINSTALLED_APPS
setting like this::INSTALLED_APPS = [ ... 'parser_app', 'rest_framework', 'corsheaders', ]
-
Add SITE_URL in setting::
SITE_URL = 'http://yoursiteurl.com'
-
Implement Django Dependancy Settings:
Such as:
MIDDLEWARE = [ ... 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ] REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', 'rest_framework.permissions.IsAuthenticatedOrReadOnly', ), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', ), } CORS_ORIGIN_ALLOW_ALL = True
-
parser_app is a api based packege. Include it's URLconf in your project urls.py exactly like below::
path('api/', include('parser_app.urls')),
-
Run
python manage.py migrate
to create the parser_app models.
- Register your models in
admin.py
file. A demo model and it's admin.py file is given below::class Area(models.Model): code = models.IntegerField( null=False, blank=False, unique=True, verbose_name="Location Code", db_column='Code', default=0) name = models.CharField(null=False, blank=False, max_length=50, db_column='Name', default='') location_type = models.CharField( choices=LOCATION_TYPE, default='', max_length=20, verbose_name="Location type", db_column='LocationType') parent_code = models.IntegerField(null=True, blank=True, default=0, verbose_name="Parent Code", db_column='ParentCode') class Meta: verbose_name_plural = "Area" def __str__(self): return str(self.code)
from django.contrib import admin # Register your models here. from .models import Area from parser_app.parser import DataParser DataParser.register_model(Area)
- Start the development server
python manage.py runserver 0.0.0.0:8000
and visit http://yoursiteurl:8000/admin/. After logging in, you will see a new model namedRegistered Models
underparser_app
section. All your registered models will be listed here.
- Click on the
Registred Models
that is located insideparser_app
section. - You will see the
Area
model listed there. - Select the check box and then click on the django admin action panel.
- Select
import with parser app
and click ongo
. - Select your desired Excel file and submit.
- In the next step, you will be given the model's columns and the columns available in the Excel file.
- Map the columns to specify which column of the Excel file means which column of the Model and then submit.
- You will the shown the estimated data type validation errors in the next step. For example, your model field accept
int
value but your file containsstring
. - If no validation errors are found, you will be asked to confirm upload.
- After confirmation, your data wil be imported in the desired model (in case of this example,
Area
model).