Prevent test migrations from being run on a specific database - django

I have a Django application connectinig to 2 database.
Both of them as seen by Django as PostgreSQL database but one of them is not a PostgreSQL (but uses the PostgreSQL binary protocol so it uses the same driver (CockroachDB).
Currently it looks like during test runs, Django try to run the migration on both databases.
How can I avoid that ? I still need the database to be accessible during the tests, but no to run the migration (As it's not compatible for now and the migrations are run outside from Django)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
...
},
'livedb': {
'ENGINE': 'django.db.backends.postgresql',
...
}
}

You can add a database router that prevents migration. Assuming livedb is the DB you do not want to migrate, it would look like this:
class LiveDBRouter:
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Do not allow migrations on livedb.
"""
return db != 'livedb'
Then you configure that router:
DATABASE_ROUTERS = ['path.to.LiveDBRouter']

Related

Create Django admin panel where project is created using another language

I have created a project using Golang. I have used PostgreSQL as the database. Now I want to make an admin panel for this project using Django. I want directly reflect the database tables in the Django admin panel. As I am not running the project under the Django server and I don't have any apps and models, how can I show them in the admin panel?
What you want is to use an existing DB to be managed by the DJango admin. To do this you must create a django project and create the models for each table. This is very tedious so there is an official process so you don't have to do all this work and it is done automatically.
$ python manage.py inspectdb
https://docs.djangoproject.com/en/4.1/howto/legacy-databases/
First create an empty project in django then in settings.py set your database as postgreSQL
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'customers',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
make sure to create superuser for login in admin panel and proper urls for the same.
Add models in models.py and register it in admin.py
P.S - make sure you have installed psycopg2 using pip command.

How to integrate django with existing Postgres database

To use an existing database in Django, you need to have a model for each table. But creating models for existing tables manually is just too much work. However, there's no need to do that, since Django has a builtin tool to solve this exact problem.
Reference article linked here: how-to-integrate-django-with-existing-database
Edit settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '<name>',
'USER': '<user>',
'PASSWORD': '<password>',
'HOST': '<host>',
'PORT': '<port>',
}
}
Generate models for linked existing database tables.
python manage.py inspectdb > models.py
Tweak your tables according to your preferences. Copy all tables and add them to your app models.py
Now create initial migrations for existing tables
python manage.py makemigrations
Run the migrate command to apply the migrations, Use --fake-initial option that applies the migrations where it's possible and skips the migrations where the tables are already there:
python manage.py migrate --fake-initial
At this point, any new changes to the model structure and subsequent migrations would work as if Django managed the database since its inception
Thanks to: Dima Knivets

How to connect to my heroku database from localhost?

I'm new in django and trying to connect to my heroku database from my localhosted django app.
I've followed the instructions on heroku website but it's seems django won't understand that i'm trying to access an external database.
i get this error
relation "myAppName_user" does not exist
LINE 1: INSERT INTO "myAppName_user" ("title", "content") VALUES
('Beat...
However i never created or never intented to create a table named myAppName_user
I'm just trying to access the table user in my heroku postgres db but i don't know why it tries with myAppName_user
i just explicitly added myAppName in the INSTALLED_APPS config as django throws an error if it's not the case.
My DATABASE config :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
DATABASES['default'] =
dj_database_url.config(default='postgres://xxxxx')
I want to test my database from my localhost. I'm doing this easily with Node JS but i can't find the solution in django.
Any suggestions ?
EDIT
from __future__ import unicode_literals
from django.db import models
class Fruits(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
def __str__(self):
return self.name

Mezzanine: where is the database created by `manage.py createdb`

I have the following database settings in Mezzanine:
DATABASES = {
"default": {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lucidDB',
'USER': 'lucid',
'PASSWORD': 'xxxxxxxx',
'HOST': 'localhost',
'PORT': '',
}
}
I run the command python manage.py createdb and then answer yes to the question Would you like to fake initial migrations? (yes/no):.
Note: south was installed.
My questions:
I checked in postgres (postgres# psql --> postgres=# \l), but didn't find the database lucidDB. However, the system runs OK. Where exactly is the database created?
What does it mean by fake migration?
I didn't run syncdb or makemigrations --> migrate yet, why the system worked?
You didn't find the LucidDB in postgres because it wasn't created, you need to create it with the postgres shell see here for detailed description.
Faking a migration is marking it as complete without actually changing the database schema, it will simply add an entry in the migrationhistory database, see detailed description here.
On the Mezzanine docs it is stated that createdb is a shortcut for syncdb and migrate commands, see here for detailed description.
Hope this helps!

In Django multiple databases should not creat a test database by manage.py on 1 specific database

In Django I'm using multple databases, but by testing the functionality 1 database of the multiple database should not create a test database if you run manage.py test **. How can I solve this issue.
In your settings.py you can tell Django what database to use when testing. Here is the code to do so if you would like to use SQLite for example:
settings.py:
if 'test' in sys.argv:
DATABASES['default'] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'tests.db',
}
Django creates a new database every time you run your manage.py test, so you would want to use fixtures in your test.py file to preload you database with data before running tests (if you need it preloaded with data).
To generate a fixture for the User model for example, use this code:
python manage.py dumpdata auth.User --indent=2 > app_name/fixtures/admin.json
To use fixtures you would structure your tests.py like so:
tests.py:
from django.test import TestCase
class MyTests(TestCase):
fixtures = [admin.json]
def setUp(self):
# other setup code here
def test_the_obvious(self):
assert True == True