Packaging Stand-Alone Django Apps: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "category:Django Category:Python Category:Web Development == Directory structure == The app goes in a directory outside of any Django web project. If the app is `a...")
 
Line 29: Line 29:


== Using the package ==
== Using the package ==
=== Installing the package ===


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 37: Line 39:


It can now be referenced in other Django projects.
It can now be referenced in other Django projects.
=== Using the package in a project ===
Assuming that the package is
<syntaxhighlight lang="text">
|
+-- django-addresses
      |
      +-- addresses
      |    |
      |    +- [...]
      |
      [...]
</syntaxhighlight>
In the project's `settings.py`:
<syntaxhighlight lang="python">
    INSTALLED_APPS = (
        ...
        'addresses',
    )
</syntaxhighlight>
Then from the web project root:
<syntaxhighlight lang="bash">
$ python manage.py migrate
</syntaxhighlight>
To reference the objects in a model within the web project:
<syntaxhighlight lang="python">
class Person(models.Model):
    address = models.ForeignKey('addresses.Address')
    # ...
</syntaxhighlight>


== Uninstalling the package ==
== Uninstalling the package ==

Revision as of 00:28, 24 March 2015

Directory structure

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.)

Building the package

$ python setup.py sdist
  • Run from the package root directory.
  • Creates a dist directory which contains the (zipped) package.

Using the package

Installing the package

$ pip install --user django-addresses/dist/django-addresses-0.1.tar.gz

This installs the package on a system as a user library.

It can now be referenced in other Django projects.

Using the package in a project

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 migrate

To reference the objects in a model within the web project:

class Person(models.Model):
    address = models.ForeignKey('addresses.Address')
    # ...

Uninstalling the package

$ pip uninstall django-addresses

Notes

  1. How to Write Reusable Apps (Django documentation)