Packaging Stand-Alone Django Apps: Difference between revisions
| (9 intermediate revisions by 2 users not shown) | |||
| Line 6: | Line 6: | ||
Try to avoid naming conflicts, of course. (Check resources like PyPI.) | Try to avoid naming conflicts, of course. (Check resources like PyPI.) | ||
* Create `README. | * Create `README.md` | ||
** In the package root, e.g. `littled-addresses/README. | ** In the package root, e.g. `littled-addresses/README.md`. | ||
** Sample contents at [https://docs.djangoproject.com/en/1.7/intro/reusable-apps/#packaging-your-app How to Write Reusable Apps—Packaging your app] | ** Sample contents at [https://docs.djangoproject.com/en/1.7/intro/reusable-apps/#packaging-your-app How to Write Reusable Apps—Packaging your app] | ||
* Create `LICENSE` file in the package root. | * Create `LICENSE` file in the package root. | ||
| Line 20: | Line 20: | ||
== Building the package == | == Building the package == | ||
<div class="alert alert-warning">Before committing changes to the package, make sure to update the version if needed.</div> | |||
<syntaxhighlight lang="text"> | |||
| | |||
+- [package_root] | |||
| | |||
+- [package_dir] | |||
| | | |||
| +- __init__.py > __version__ | |||
| | |||
+- [...] | |||
| | |||
+- setup.py > setup.version | |||
</syntaxhighlight> | |||
Command to build the package: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
$ python setup.py sdist | $ python setup.py sdist --formats=gztar,zip | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 29: | Line 46: | ||
== Using the package == | == Using the package == | ||
<div class="alert alert-warning">Follow the instructions for [[#Upgrading_the_package|upgrading the package]] if the package has already been installed.</div> | |||
=== Installing the package === | === Installing the package === | ||
<syntaxhighlight lang=" | Push the package distribution to GitHub. (For development purposes, not strictly necessary for installation.) | ||
Upload the zipped distribution to Amazon S3 bucket `[https://console.aws.amazon.com/s3/home?region=us-west-2#&bucket=nrose-django-packages&prefix= nrose-django-packages]` | |||
==== Command line ==== | |||
`npm install https://s3-us-west-2.amazonaws.com/nrose-django-packages/django-jinja2-gtm-0.1.1.zip` | |||
==== requirements.txt ==== | |||
Add the URL on its own line in the `requirements.txt` file: | |||
<syntaxhighlight lang="text"> | |||
https://s3-us-west-2.amazonaws.com/nrose-django-packages/django-jinja2-gtm-0.1.1.zip | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Then to install with `pip`: | |||
<syntaxhighlight lang="bash"> | |||
$ pip install -r requirements.txt | |||
</syntaxhighlight> | |||
=== Upgrading the package === | === Upgrading the package === | ||
Update the version number in `requirements.txt`. | |||
Run `pip install -r requirements.txt` | |||
=== Using the package in a project === | === Using the package in a project === | ||
| Line 53: | Line 84: | ||
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
|- django-addresses | |||
| | | |||
| |- addresses | |||
| | | | |||
| | `-- [...] | |||
| | | |||
| `-- [...] | |||
| | |||
`-- [...] | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 75: | Line 107: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
$ python manage.py migrate | $ python manage.py makemigrations addresses | ||
$ python manage.py migrate addresses | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 84: | Line 117: | ||
# ... | # ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Resetting migrations === | |||
In the case that migrations have been made, and you want to rebuild the database objects from scratch: | |||
<syntaxhighlight lang="bash"> | |||
$ python manage.py migrate --fake addresses zero | |||
$ python manage.py migrate addresses | |||
</syntaxhighlight> | |||
* The first command sets the migration counter to before the initial (`0001`) migration. | |||
* The 2nd command migrates the models at the state of the latest migration.<ref>[http://stackoverflow.com/a/27008518 How to reset migrations in Django 1.7?] (Stackoverflow)</ref> | |||
== Uninstalling the package == | == Uninstalling the package == | ||
| Line 91: | Line 136: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<p class="alert-warning">Uninstalling a stand-along package will remove the migrations for that package. Without the migrations, Django projects won't be able to sync to any changes to the app's models. It's better to upgrade than to uninstall and install.<ref>[https://docs.djangoproject.com/en/1.7/topics/migrations/ Migrations] (Django documentation)</ref></p> | <p class="alert alert-warning">Uninstalling a stand-along package will remove the migrations for that package. Without the migrations, Django projects won't be able to sync to any changes to the app's models. It's better to upgrade than to uninstall and install.<ref>[https://docs.djangoproject.com/en/1.7/topics/migrations/ Migrations] (Django documentation)</ref></p> | ||
== Notes == | == Notes == | ||
<references /> | <references /> | ||
Latest revision as of 03:05, 15 March 2018
Directory structure[edit]
The app goes in a directory outside of any Django web project. If the app is addresses, structure it littled-addresses/addresses/.[1]
Try to avoid naming conflicts, of course. (Check resources like PyPI.)
- Create
README.md- In the package root, e.g.
littled-addresses/README.md. - Sample contents at How to Write Reusable Apps—Packaging your app
- In the package root, e.g.
- Create
LICENSEfile in the package root.- Django itself uses the BSD license.
- Create
setup.pyin the project root.- Sample at How to Write Reusable Apps—Packaging your app
- More complete documentation at setuptools docs.
- Create
MANIFEST.inin the project root.- This includes files other than Python modules and packages.
- Create a
docsdirectory- Include it in
MANIFEST.inwithrecursive-include docs *
- Include it in
Building the package[edit]
|
+- [package_root]
|
+- [package_dir]
| |
| +- __init__.py > __version__
|
+- [...]
|
+- setup.py > setup.version
Command to build the package:
$ python setup.py sdist --formats=gztar,zip
- Run from the package root directory.
- Creates a
distdirectory which contains the (zipped) package.
Using the package[edit]
Installing the package[edit]
Push the package distribution to GitHub. (For development purposes, not strictly necessary for installation.)
Upload the zipped distribution to Amazon S3 bucket nrose-django-packages
Command line[edit]
npm install https://s3-us-west-2.amazonaws.com/nrose-django-packages/django-jinja2-gtm-0.1.1.zip
requirements.txt[edit]
Add the URL on its own line in the requirements.txt file:
https://s3-us-west-2.amazonaws.com/nrose-django-packages/django-jinja2-gtm-0.1.1.zip
Then to install with pip:
$ pip install -r requirements.txt
Upgrading the package[edit]
Update the version number in requirements.txt.
Run pip install -r requirements.txt
Using the package in a project[edit]
Assuming that the package is
|- django-addresses | | | |- addresses | | | | | `-- [...] | | | `-- [...] | `-- [...]
In the project's settings.py:
INSTALLED_APPS = (
...
'addresses',
)
Then from the web project root:
$ python manage.py makemigrations addresses $ python manage.py migrate addresses
To reference the objects in a model within the web project:
class Person(models.Model):
address = models.ForeignKey('addresses.Address')
# ...
Resetting migrations[edit]
In the case that migrations have been made, and you want to rebuild the database objects from scratch:
$ python manage.py migrate --fake addresses zero $ python manage.py migrate addresses
- The first command sets the migration counter to before the initial (
0001) migration. - The 2nd command migrates the models at the state of the latest migration.[2]
Uninstalling the package[edit]
$ pip uninstall django-addresses
Uninstalling a stand-along package will remove the migrations for that package. Without the migrations, Django projects won't be able to sync to any changes to the app's models. It's better to upgrade than to uninstall and install.[3]
Notes[edit]
- ↑ How to Write Reusable Apps (Django documentation)
- ↑ How to reset migrations in Django 1.7? (Stackoverflow)
- ↑ Migrations (Django documentation)