How to do it...

Learn the basics of using the Django shell by following these instructions:

  1. Run the Django shell by typing the following command:
(myproject_env)$ python3 manage.py shell

If you have installed IPython or bpython, it will automatically become the default interface when you are entering the shell. You can also use a particular interface by adding the -i <interface> option to the preceding command. The prompt will change, according to which interface you use. The following screenshot shows what an IPython shell might look like, starting with In [1] as the prompt:

If you use bpython, the shell will be shown with the >>> prompt, along with code highlighting and text autocompletion when typing, as follows:

The default Python interface shell looks as follows, also using the >>> prompt, but with a preamble that provides information about the system:

  1. Now, you can import classes, functions, or variables, and play around with them. For example, to see the version of an installed module, you can import the module and then try to read its __version__, VERSION, or version properties (shown using bpython, and demonstrating both its highlighting and autocompletion features), as follows:

  1. To get a comprehensive description of a module, class, function, method, keyword, or documentation topic, use the help() function. You can either pass a string with the path to a specific entity, or the entity itself, as follows:
>>> help("django.forms")

This will open the help page for the django.forms module. Use the arrow keys to scroll the page up and down. Press Q to get back to the shell.

If you run help() without the parameters, it opens an interactive help page. There, you can enter any path of a module, class, function, and so onto, and get information on what it does and how to use it. To quit the interactive help, press Ctrl + D.
  1. The following is an example of passing an entity to the help() function, shown with IPython:

Doing so will open a help page for the ModelForm class, as follows:

  1. To quickly see what fields and values are available for a model instance, use the __dict__ attribute. Also, use the pprint() function to get the dictionaries printed in a more readable format (not just one long line), as shown in the following screenshot. Note that when we are using __dict__, we don't get many-to-many relationships. However, this might be enough for a quick overview of the fields and values:

  1. To get all of the available properties and methods of an object, you can use the dir() function, as follows:

To get these attributes printed one per line, you can use the following code:

  1. The Django shell is useful for experimenting with QuerySets or regular expressions, before putting them into your model methods, views, or management commands. For example, to check the email validation regular expression, you can type the following into the Django shell:
>>> import re
>>> email_pattern = re.compile(r"[^@]+@[^@]+.[^@]+")
>>> email_pattern.match("[email protected]")
<_sre.SRE_Match object at 0x1075681d0>
  1. If you want to try out different QuerySets, you need to execute the setup of the models and apps in your project, shown as follows:
>>> import django
>>> django.setup()
>>> from django.contrib.auth.models import User
>>> User.objects.filter(groups__name="Editors")
[<User: admin>]
  1. To exit the Django shell, press Ctrl + D, or type the following command:
>>> exit()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.79.60