Populate a DataBase from an API in Django - python

I am building a Django project with a postgresql in it. This databasa has to be populated from an API. This database doesn't have to be updated.
As I understood, the tables inside my database have to be created in my Django models. But where should I populate it?
In a script outside Django or inside my app views?

I did find a way to populate my DataBase from Django itself. I added a custom populate_db command in managment/command that runs a scritp that populate my DataBase from an external API.
Then I just have to run python3 manage.py populate_db

Related

How to restore a dump database backup for a Django project with existing models and migrations?

Setting up a mysql database from an existing dump is very easy
# open prompt cd into the directory where the dump is located
# login to mysql
create database mydb
use mydb
source mydb.sql
And all is done!
But the problem comes with Django. Django has troubles with existing schemes and data that have not been created from models and migration system. And in Django, everything must come from models and migrations.
I perfectly know that I have this option
python manage.py inspectdb > models.py
But this is only suitable when creating a blank new Django project using existing database.
What if I want to run an existing Django project to another machine and the Django project uses a database? The project already has existing models with some manage=false set, I cannot simply inspectdb it. And if I restore the database outside Django, the Django ORM will no longer recognize it.
Then what is the correct way to restore a database from a database backup for a project with existing models and migrations?
If the database that you are restoring was previously managed by django, you should encounter no problems simply restoring the database with your three-step process. Django's migration history is stored as a table in any database that it manages. As long as that table makes it over, your new migrations going forward should work fine.

Storing the Django project with the PostgreSQL database on GitHub

I changed the database from SQLite3 to PostgreSQL in my Django project. Is it possible to store my new database in the GitHub repository so that after cloning and running by the command
python manage.py runserver
the project has started with the whole database?
You cannot save the database as such, you can instead create fixtures to be run. Whenever someone will clone the project, he/she can simple run those fixtures to populate the database.

Advice on how to handle PostgreSQL database in Django app?

For some background, I worked on a Django app that had a SQLite3 database. I just created models in Django using the python manage.py startapp model_name, and just directly manipulated the database by creating API endpoints in python. I would then make API requests and change my database like that.
But this time, I've created a Django app that has a PostgreSQL database. I managed to connect my database to Django after figuring out how to install psycopg2. Then, I created a Users table in PostgreSQL through the command line. I want to get this table to show on my Django admin site, and hopefully repeat the same method for the other app. But the Django admin site can render only Django models that have been registered to the admin. So now, I'm wondering if there is a better method for working with PostgreSQL in Django.
I saw SO posts suggesting inspectdb, but this creates a large models.py file generating other classes for my app. I also couldn't successfully register the Users model to the Django admin site. I am now curious if I am working in the opposite direction. Should I be generating User models in Django and then from there, generate my PostgreSQL database?
Basically, can someone explain how to work with a PostgreSQL database in a Django app?
Can you be more specific with your question?
If you are confused on how to register the User table in django admin, you can register it in admin.py. But the method of registering depends on the type of User model you are using. You can see the docs for Custom User model.
And to get the user table in your database, you need to do migrate:
python manage.py makemigrations
python manage.py migrate
Let me know if you need any help.

Running Django Commands on a different server

Right now I have a django website. I manage to programmatically populate the database using django commands and everything works fine.
I now want to have a dedicated server for populating the database programmatically but I do not want to give up using django commands as thei are very powerful in my opinion.
I am using aws elastic beanstalk and an rds db instance, so I know that I could simply create my scripts (the ones I use to populate the db) in pure python, but I am wondering if there is a nice way to do the same using Django.
I thought of simply setting up a new Django project that connects to the same RDS database, but this would mean that I need to copy all the models/settings every time a make a change to my main website... I did not really seem ideal.
Any thoughts?

how to perform migration for django table created without any app

I have a django project in which I have not created any app, I directly wrote models for the project in models.py file inside project folder.
But whenever I perform
python manage makemigrations
it says : No changes detected
so when I perform
python manage migrate
it says : No migrations to apply.
So is there any different way to perform migrations if you are directly writing your models without creating any App for django project
You should add your app to django INSTALLED_APPS settings, if not, then, you dont have an django app.
migration on django is an app basis.
migrations are created and run on a per-app basis, https://docs.djangoproject.com/en/1.8/topics/migrations/
so you should create an app and add it to INSTALLED_APPS as levi suggest. However, it is possible if you're kind a want to only use one app to manage all the models for your db under models.py, BUT it's messy.
i recommend to use the migration on app basis.

Categories

Resources