What should I do with plugin-created migrations? - django

I have, as usual the <project>/<app>/migrations folder that I added to the version control for deployment. Since recently I am also using django-auditlog, which creates its own migrations in <project>/env/Lib/site-packages/auditlog/migrations. These migrations are applied just like my own ones. So I wonder: should I also add them to VCS and deploy them?

No. You shouldn't deploy these migrations. In general you shouldn't deploy any third-party migrations.
The reason is simple. Third-party packages have their initial migrations inside their migrations directory. Each time you create a migration that depends on a third-party package, this migration will live in your app's migrations dir (not inside the package one).
So, when you first deploy your code and run pip install -r requirements.txt on your server, then all 3rd party packages will be installed and when you do ./manage.py migrate then Django will look at your INSTALLED_APPS and execute all migrations for each one in this list.
The same thing happens with any third-party package, just like Django itself. Suppose you have in your requirements.txt file only Django==1.10.6. When you deploy this to the server and do pip install -r requirements.txt, django-admin createproject myproject and then ./manage.py migrate, then your database will be "filled" with all Django-specific tables that has built-in (i.e auth, sessions, contenttypes etc) and are enabled in the INSTALLED_APPS setting list.
The one and only that you should deploy is everything under your project directory (requirements.txt, manage.py, robots.txt etc) and not your 3rd party apps which may live either inside your project (under a .virtualenv dir, probably) or in a separate .virtualenv dir outside of your project.
An analogous to your question is node_modules for nodejs. node_modules directory is never deployed to VCS (because it can get quite large in size etc), instead only the package.json is, because knowing what dependencies (package.json/requirements.txt) your project needs, then your project can "reconstruct" itself from zero.

Related

How to runserver and see website for github project files

New to Django, I am trying to view the website with its associated functionality generated by the project files here:
https://github.com/tomwalker/django_quiz
When I use the usual: manage.py runserver on the command prompt, it says that there is no manage.py file, and there isn't.
Worryingly, I followed the instructions for installation which suggested: Run pip install -r requirements.txt
...and my computer proceeded to de-install Django 2.0? What is that about, and can anyone explain how to restore settings if I messed them up completely.
The second part of the instructions for installation asks to change something in the INSTALLED_APPS and urls.py section, but where? There is nothing in the root directory and it doesn't specify which folder/app to do this in?
I don't quite understand how to "run" (see/view) these files and see this quiz app in process on my local host. What do I need to add? Why is the manage.py file not included?
Any explanation or something to point me in the right direction would be appreciated
The github project contains only Django apps. Not whole project. You need to integrate this in your Django project. You can run it by following below steps.
Create New Django Project
Clone github repo in your project. Run following commands in your project directory.
git clone https://github.com/tomwalker/django_quiz.git
mv django_quiz/* .
rm -rf django_quiz
Add essay, true_false, quiz, multichoice in your installed apps
Install requirements with pip install -r requirements.txt
Create Migrations
Run Migrations
Add url(r'^q/', include('quiz.urls')) in your project urls.
Run server with python manage.py runserver

Deployment script to accommodate up-gradation of 3rd party apps and (fake) migration of models

In fabfile.py, I use something similar to the following to install dependencies and to migrate the changes to the new model
run("pip install -r ../deployment/pip/deploy.txt")
run("python manage.py migrate --settings=project.settings.prod")
When I upgrade the 3rd party apps which have migrations in the current version, but did not have in the previous one, my understanding is that I have to run ./manage.py migrate --fake <app_name>.
What would be a good solution with which a single deployment script (with Fabric) could pull the code from GIT repo, install new dependencies, take care of migrations and IF there are 3rd party apps that started using migrations recently, then run fake migrations on those.
Any pointers would be highly appreciated.
Thank you

How to manage django missing migration files of 3rd party apps?

I have a problem with migrations on heroku.
I have just upgraded version of django from 1.8.1 to 1.8.2 in my project on heroku, because of some missing migrations:
https://github.com/django/django/blob/1.8/django/contrib/auth/migrations/0005_alter_user_last_login_null.py#L14-L18
After this upgrade, on my local project I was forced to do standard procedure:
python manage.py makemigrations
python manage.py migrate
Of course this new, generated migrations are outside of my git repository. If I will run heroku run this will generate new migrations in new dyno, so without any impact on my current slug.
What is the best solution for this situation? How to generate migrations or add missing migrations of django or 3rd part libs when e.g. you are doing upgrade of the libs? What is the best working strategy?
Answer is quiet trivial, could be useful for others.
To use MIGRATION_MODULES is the correct answer.
https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-MIGRATION_MODULES

How To Preserve Changes To Pip Installed Django Apps After Deployment

I made custom modification to one of the Django apps in my requirements.txt, the problem is that after deployment I get errors because the I get fresh pip installs from the requirement.txt and the changes I made only work locally. What is the right way to modify pip installed Django apps locally and have those changes also reflect in the deployment environment?
You could host a fork of the library you want to change somewhere like GitHub, and have your requirements.txt point to that particular change. http://codeinthehole.com/writing/using-pip-and-requirementstxt-to-install-from-the-head-of-a-github-branch/ has a good overview of having a pip requirements file point to a source code repository.

How to import a Django app from Git into a project

I want to include a Django app into the project I'm working on. The app is hosted on Github ( https://github.com/lmorchard/django-badger ). As well as the app's directory containing the goodies, there are some files in the root - README, LICENCE, TODO and setup.py. If I clone the app into my project's root directory, the app folder will be in the correct place, but those root files will be in my project's root. How can I add the app while still tracking the upstream code in Github?
I had a similar issue where I was working on two independent projects where both were in a repo, and one of them used the other as an app:
Create a virtualenv and install all dependencies for both projects. I usually like to have a virtualenv for each project/repo but in this case you need one env which can execute Python from both repos.
Clone both repos to independent location. Do not clone the depending app inside the other project. Your file-structure then might look like this (assuming Django 1.3 project layout):
project/
manage.py
project/
__init__.py
settings.py
...
...
app/
README
...
app/
__init__.py
models.py
...
And final step is to create a symlink (or shortcut on Windows) from the app directory which has __init__.py in it to the project path.
$ ln -s /abs/path/to/app/app /abs/path/to/project/
Now you can use the virtualenv to run the project!
The final result is that you have two independent repos however one of projects is using the other project without directly copying the code, hence allowing you to maintain two repos.
U can install it by running
python setup.py
or through pip
sudo pip install -e git+https://github.com/lmorchard/django-badger#egg=django-badger
Clone the repository from github using git://github.com/lmorchard/django-badger.git. Then open the cloned folder in terminal. Install the app using the command sudo python setup.py install. This will work good. If you want to have the app included in your project, create a folder named badger(or anything you wish) and copy the installed app from dist-packages to created folder.