5

I have defined my model and all in Django and if a user is registering via my application the user is well register in the database. The problem is I have a file containing a JSON with plenty of users. I want to do a job that allow me to read this file and insert all the user in my database.

What is the best way to do it? How to connect to the Database without using Django?

1
  • 1
    you can use module sqlite3, sqlalchemy, peewee. Or you can try to convert JSON into CSV and then import directly to database using some Database Viewer/Editor. I don'ty know if they can import data directly from JSON - ie. DB Browser for SQLite Commented Jan 7, 2017 at 7:30

3 Answers 3

6

Why do you want to connect to the database without Django? I would do it using Django, but you need to initialize Django, to be able to run scripts against it. Something like this should do it:

import os, sys
sys.path.append('/path/to/django/project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
import django
django.setup()
from django.contrib.auth.models import User

for row in data:
    user = User.objects.create_user(username=row['username'])

/path/to/django/project is the path to the folder where the directory containing your settings is. For example with hierarchy of:

/projects/my_project
/projects/my_project/my_app
/projects/my_project/my_project/setting.py

you you need to append /projects/my_project.

If instead of the Django User model you need to work with a custom model, instead of

from django.contrib.auth.models import User

do

from app_name.models import ModelName
Sign up to request clarification or add additional context in comments.

6 Comments

It's just that I don't really know where to put that job so I have a folder: project wich contains: django_project (Where my app and my settings are) and a folder job (Where I do all my jobs, crawler and stuff to aliment my database)
With the code above you explicitly initialize your Django environment in the script, so it doesn't matter where the script resides. You can put it in the jobs-folder if you like.
"/path/to/django/project" this path must end-up on my settings.py where I config my database?
No, it's one level below that. In other words, it's path to the directory where the folder containing your settings file is.
Try from app_name.models import Dater
|
2

If you want to insert data without going through django's views, use the shell

Run python manage.py shell in your app's directory.

Then import JSON data through python's json.loads()and loop through the data creating and saving models.

You can also use bulk_create to create models.

YourCustomUserModel.objects.bulk_create([
    YourCustomUserModel(username=datarow.username)
    for datarow in data
])

Comments

1

You can use Django's native manage.py command instead:

Inside of your yourapp directory, create the tree structure (including the folder management), like so:

yourapp
├── admin.py
├── __init__.py
├── management
│   ├── commands
│   │   ├── __init__.py
│   │   └── populate_db.py
│   └── __init__.py
├── models.py
... other files

Then, inside the file populate_db.py write the appropriate code to populate your database, e.g.:

from django.core.management.base import BaseCommand
from yourapp.models import Post, Tag

class Command(BaseCommand):
    args = '<foo bar ...>'
    help = 'our help string comes here'

    def _create_tags(self):
        tlisp = Tag(name='Python')
        tlisp.save()

        tjava = Tag(name='Java')
        tjava.save()

    def handle(self, *args, **options):
        self._create_tags()

With this setup you can run $ python manage.py populate_db to automatically populate your database.

And of course you can expand this code to read input from e.g. a JSON file that you can keep in a data folder of your app. This allows you to commit non-sensible data that should be part of your app to version control without having to compromise the database (and thereby potentially your superuser access).


Credit for this elegant solution goes to Eli Bendersky: https://eli.thegreenplace.net/2014/02/15/programmatically-populating-a-django-database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.