Testing Django Models on the Command Line
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.