Advice on how to handle PostgreSQL database in Django app? - python

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.

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.

Populate a DataBase from an API in Django

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

Override Django Password Reset

I was able to set up a Custom User using AbstractUser.
I'm trying to find a way to use Django's Password Reset Class to reset my users. So I implemented it the way it is normally used but I get the following error:
ProgrammingError: column app_user.date_joined does not exist
or
app_user.is_active does not exist
I shouldn't have to add any of these fields to my AbstractUser and I'm sure there is a way to override the Django auth. Does anyone know how?
EDIT: I've already migrated all my databases.
Your database is not migrated.
You have to run python manage.py migrate to migrate the DB.
If that was never run, than you have to make the migrations before applying them.
python manage.py makemigrations
python manage.py migrate

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.

Django with MongoDB

OK.. i am starting a project in django 1.4 and i want MongoDB as my backend. after a half a day of google search, i figured out that mongoengine is a best option(as it is an active project and provides a django like orm)
Now the problem is
1. I cant find any good step-by-step setup guide to integrate mongoengine with a django project.
I understand, using mongoengine means that i am replacing django orm and there is no need to do syncdb. now this project have a multi-tenant architecture (*.domain.com) which i am gonna resolve using a middleware..also a considerable part of this project will work on django admin.
Question: will replacing django orm with mongoengine going to affect django admin and other operations(such as middleware, authentication etc.) ?
I am open to suggestions and criticism as well.
Django Admin is designed to work with the Django ORM only. Using MongoEngine and no Django ORM will mean you don't get the automatic admin interface. Other middleware might use the Django ORM or be sufficiently abstracted enough to allow you to plugin MongoEngine - eg: Sessions and Authentication.
There are some helpers for Django in MongoEngine - but its by no means complete or designed to be a drop in replacement for the Django ORM.
For more information see this presentation from Django Conf Finland: http://staltz.github.io/djangoconfi-mongoengine
Just in case, situation has changed and there is a solution now for this problem, namely django-mongoadmin.
A Guide to integrating Django with MongoDB
The way to connect Django with MongoDB by adding just one line of code:
First install djongo:
pip install djongo
Then run your migrations:
manage.py make migrations
manage.py migrate
and finally add to your settings file:
DATABASES = {
‘default’: {
‘ENGINE’: ‘djongo’,
‘NAME’: ‘your-db-name’,
}
}
It is as simple as that!
If you want to manipulate MongoDB using Django Admin, simply fire it up:
manage.py runserver
Goto: http://localhost:8000/admin/
Manipulate your embedded models as shown in this screenshot:
For more information do checkout the djongo documentation.
You should definitely consider the pros and cons of using a NEW framework (like MongoEngine) vs using inbuilt Django ORM. Do read at this tutorial before considering to adopt MongoEngine as suggested by other knowledgeable members! No offence!
Let me know if you agree with this approach in the comments :)

Categories

Resources