Testing Django Models on the Command Line: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "Category:Django Category:Python Category:Web Development First, `cd` to the project directory. Make sure the `DJANGO_SETTINGS_MODULE` environment variable is set....")
(No difference)

Revision as of 22:56, 1 December 2015

First, cd to the project directory.

Make sure the DJANGO_SETTINGS_MODULE environment variable is set. (See Setting Environment Variables in Powershell.)

Set DJANGO_SETTINGS_MODULE to the path to settings.py in the Django project that is being tested. E.g.:

> $env:DJANGO_SETTINGS_MODULE = 'myproject.settings'

Start up python cli:

> python

Example of fetching a model:

>>> from clients.models import Client
>>> from contact_info.models import Address
>>> c1 = Client.objects.get(pk=1)
>>> c1.name
>>> from django.core.exceptions import ObjectDoesNotExist
>>> try:
...     c2 = Client.objects.get(pk=1,display_on_frontend=True)
... except ObjectDoesNotExist:
...     print('not found.')
...
not found.