Introduction
In this article, the main purpose is to be able to retrieve or to get the value from a property or an attribute. Specifically, it is the property or the attribute from a Django Model exist in Django application. As for the preparation to model and to simulate the process, the following are the steps for performing the preparation :
-
As a first preparation, just make sure the device have Python tool interpreter exist in it. For more information, just look in How to Install Python in Microsoft Windows, How to Install Python 3.10 in Microsoft Windows or How to Install Python in Microsoft Windows 11
-
The second preparation, just make sure that ‘pip’ tool also exist in it. As for more information, just look in How to Install pip in Microsoft Windows or How to Install pip in Microsoft Windows 11.
-
The third one, just create a virtual python environment and make sure to activate it. And for information about python virtual environment, look in How to Create an Isolated Python Environment, How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows or in How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows.
-
When virtual python environment has already active, just install Django library. Look in How to Install Django in Microsoft Windows in order to do it.
-
Last preparation, create Django project and also a Django application inside of it. Do not forget to look in How to Create a Django Application inside a Django Project in Microsoft Windows in order to do it.
How to Get Property or Attribute Value of a Django Object from a Django Application using Python Interactive Shell
Since the preparation is already exist, just go through the process to retrieve the value of property or attribute from a Django model. In this context, the process will run using a python interactive shell just to show it in detail. Below is the sequence of the execution :
- First of all, running the python interactive shell :
(env) C:\django\myproject>python manage.py shell Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>>
-
As an example, this article will use a Django model User which exist in package ‘django.contrib.auth.models’. So, below is a command for importing the ‘User’ Django model :
>>> from django.contrib.auth.models import User;
-
After that, try to retrieve all available User Django object from User Django Model class. Actually, retrieving object from Django Model using Python interactive shell is already exist in another article in How to get certain Object from a Django Model from a Django Application using Python Interactive Shell. Following after, just pass it to a variable with the name of ‘user_list’ as follows :
>>> user_list = User.objects.all()
-
And after that, just print the variable of the user_list to check the content of it :
>>> print(user_list); <QuerySet [<User: admin>]>
So, there is a Django User object which is ‘admin’.
-
Moving on to the next step, in order to perform the task on getting the value of the attribute or the property of a Django User object, just get one of the Django object first as follows :
>>> user = User.objects.get(pk=1)
The above command or syntax will retrieve a specific Django Object of User with the primary key ID of 1. Since there is only 1 Django Object available represented by only one record in the Django table of user, the user variable will only contain one Django Object of User.
-
Finally, print the attribute or the property of the specific Django Object of User. There are three attributes or properties for a trial attempt to get by printing it. The first one is first_name, last_name and finally username :
>>> print(user.first_name); >>> print(user.last_name); >>> print(user.username); admin >>>
As it exist in the above output, the only value available from the Django Object User of ‘admin”s attribute or property is the username’. In this context, the value of the username attribute or property from the ‘admin’ Django Object User is literally ‘admin’.