I'm starting to use the "redirects" app built into Django to replace some existing redirects I have in my urls.py. I was wondering if there was any generally accepted way to include initial data in the code base for other apps. For example, if it was for an app I created I could create a migration file that had a RunPython section where I could load some initial data. With a built-in or third-party app there doesn't seem to be any way to create a migration file to add initial data.
The best I can think of right now is to include a .sql file in my repository with the initial data and just manually import the data as I push the code to the different instances.
you can do it by using fixtures
create a folder name fixtures in your app directory
use this command to create a json file that you want to make as initial data.
python manage.py dumpdata you_app_name.model_name --indent 2 > model_name.json
copy this model_name.json to fixtures folder.
upload the code to the repo.
then after the migrate command . Type this command to load the initial data.
python manage.py loaddata model_name.json
reference
Related
My use case involves running python manage.py migrate with DATABASE_HOST=127.0.0.1 (since I use Cloud SQL Proxy). However, when the application is uploaded and is serving, the database URL needs to change to an actual remote URL.
Right now, I upload a special settings.py file that contains the localhost URL when I wish to run the migration command. When I deploy to cloud, I make sure to overwrite that file with a new one (which is essentially the entire file except the database URL is my remote db URL) and then upload it.
Is there a better way to achieve this? Something like python manage.py --database_url=127.0.0.1 migrate?
Maybe you should try making a separate file, let's say local_settings.py, in the settings.py directory. In that file copy the ALLOWED_HOSTS =["your IP"].
Then in your settings.py import it as form try: .local_settings import * except: pass
But keep the ALLOWED_HOSTS=[ ] in your settings.py as it is.
Hope it helps!
I used jq to modify a JSON file I read values from (DATABASE_HOST=127.0.0.1), then overwrote the new JSON file once I was done running migrations with the original file.
I have a Django project and I am using MySQL as default. I already have created and migrated 2 models. Now I have a SQL file. I want to create new models and populate those models with this SQL file. I am using Windows 10.
I have searched this query but did not get working solution.
I think you are looking for inspectdb. Try Integrating Django with a legacy database.
First of all create a database using your SQL file.
Then add the details of your database into settings.py file.
Just run python manage.py inspectdb command, you will see all the table structures in your new database which creates with your SQL file as a Django Models structure. (convert your database into python django model).
You can get those details as a .py by running python manage.py inspectdb > models_new.py command.
Now you have all the table structures in that models.py file which create in last step.
Then change your settings.py file for old database which you migrated earlier.
Get those details from models_new.py and add into your app_name/models.py file and save it.
Run python manage.py makemigrations and python manage.py migrate.
I found useful video tutorial which is not in English, but you can get an idea about your question how to do it.
I am using Django and created some flatpages via the admin panel, then I export them to my project in a .txt format using:
python manager.py export_flatpages 1 > my_flat_page.txt
Since I am working remotely, I need my peers to be able to quickly migrate those .txt files to their local database.
Any idea about how can I create this migration?
PS>
I already read this https://docs.djangoproject.com/es/1.9/topics/migrations/, but did not get how to relate .txt to migrations.
You can do the following to export flatpages from your db
(https://docs.djangoproject.com/es/1.9/ref/django-admin/#django-admin-dumpdata)
python manage.py dumpdata flatpages > flatpages.json
And then load this flatpages.json using
(https://docs.djangoproject.com/es/1.9/howto/initial-data/)
python manage.py loaddata flatpages.json
Every time I wipe the database of my Django app during testing (or when cloning or deployin), I have to go into /admin and set up permissions and groups. Where would I put the code that would populate the DB with them and what would it look like?
For this you can use fixtures.
For example:
python manage.py dumpdata auth > fixtures/auth.json
This will store all models of package 'auth' (Users, Groups Relations) into auth.json
After Deployment you can use the following command to load:
python manage.py loaddata auth fixtures/auth.json
This will restore your prev state of 'auth'.
Maybe it's good for you to switch to South, a very famous part of Django to migrate databases instead of recreating them.
You can provide fixtures with the initial required data and it will be automatically inserted when you syncdb. See docs
I am trying to create a development server from a production server from which I can test out new ideas.
I created a duplicate of my production server's database by dumping it using Postgres' db_dump and then imported the dump into a new database.
I then copied my production django directory and altered all .py files to refer to server_debug. rather than server in my import statements.
Using the admin interface to alter some data works in that only the development server has its data altered.
However, when I then try adding a new field in my models.py in my development server, manage.py syncdb fails to create it.
Is there something I am neglecting that could cause manage.py to refer to my production server rather than my development server?
syncdb doesn't touch tables that already exist. You need to either reset the app (easiest if you don't care about the data), modify the table manually (more of a quick hack) or use a migration app and version your models — South, for example.