How to Relate Users with Data from Database Tables in the Python Shell with Django


In this article, we explain how to relate users with data from database tables in the Python shell with Django.

What is meant by this is, say we have users on our website and the users create content, such as comments, ratings for products, etc.

These users are the creators of the comments that they write and the ratings that they give. They own the comments and the ratings.

In our simple program we'll demonstrate, we'll have users and we'll have a database table named Car.

We'll show how in the Python shell you can show the relations between users and specific data of a database table that can represent objects, such as comments, user ratings, etc.

In this specific example, we'll show demonstrate how we can relate users with Cars from the Car database table.

So, for this project, we will create users, as well as the Car database.

So, below is the file in which we create the user, as well as the Car database table.

So this is what is in our file.

In this file, we import settings from django.conf

settings is necessary because we use this to create User, which is used to link users with the Car database table.

We also import models, which is always done to create a database table in Django.

We create a single field for the Car database table called name. This will store the name of each car in the database table.

In this class that we created we create a __str__ function. The reason we do this is when we refernence the objects in the database, we want a string returned, not just a generic object, which will be shown unless we define the objects in the database table with the __str__ function. In the __str__ function, we simply return the name of the Car.

I went into the admin and created 3 cars for this database table: Car 1, Car 2, and Car 3.

This can be seen below.

Django admin 3 objects created in database table

Now we're going to go into the shell and show how to relate users with these Car objects.

So the first thing we must do is import the database table to the shell.

This is done through the following line of code.

Because the Car database table is located in the file in my Blog app, I specified the above line. However, if you have a different app name, then change the Blog to that name. If you created a different database name, change Car to the database name you created.

With this line, we now have access to the Car database table in the shell.

If we use the following line shown below, we can list out all the objects in the Car database table.

So now we've listed out all of the objects in the Car database table.

Since I created 3 car objects (Car 1, Car 2, Car 3), this is what you see in the QuerySet.

Now let's create a variable called Car1 and let's set this variable equal to Car.objects.first()

This is shown below.

So we create the variable Car1 and set it equal to the first car of the database table.

We then call Car 1 and see that it does contain the first object in our database, Car 1.

Now let's get the user of this Car 1 object.

We create a variable called Car1_user and set it equal to Car1.user

This is shown below.

So in this code we create the variable Car1_user and set it equal to Car1.user

We then call Car1_user and get, , which is my admin username in which I created the 3 car objects and assigned them to my username (dlhylton).

So this is powerful. You can see how we just started with an object in the database table and we were able to retrieve the user who created who owns or is tied to the Car object.

You see how when we called Car1_user, we got,

If we call Car1_user.username, we can get just the value of the username returned.

This is shown below.

Now we're just returned the plain username.

You can also do other stuff such as get the username's email.

This is shown in the following line.

Another powerful thing you can do is get the class of the user.

This is shown in the following code below.

Because we created the user with Django's built-in user model, we are returned the class,

What's even more powerful is that if you create a variable and assign it to the user's class, you can then get all of the users of the class.

Because there is only one user created so far, only 'dlhylton' is returned. But this list would have all of the users of this class.

So basically you just create a variable and assign it to the user's class. Then you get all objects (users) in the class.

So this is some basic things we did to relate objects in a database table with the users that are associated with the data.

Now we will do the reverse. We will start out working with the users and getting the objects that they have created or that they own.

Starting with the Users

Now we will start off working with the users and getting the objects that they own.

We do this with the following code shown below.

So to get users from Django's built in user model, we use the line, from django.contrib.auth import get_user_model

We then create a variable named User and set it equal to get_user_model()

The User variable now contains the class Django's built-in user model.

We then use the line, User.objects.all(), to get all users.

Because I only created one user, it returns, ]>

Now let's go over how we can get all of the items that dlhylton owns.

So since, dlhylton is the only user, I create another variable, dlhylton and set it equal to User.objects.first()

I then use the line, dlhylton.car_set.all(), to get all the cars that dlhylton owns.

This is shown in the code below.

So any time you want to get all of the objects that a user owns for a given database table, you call the username, followed by a dot, followed by the database name_set (in this case car, so it's car_set), followed by .all().

This gets all the objects of the Car database that belongs to the user.

So this is how you can data in a database table with user and relate users with data in database table in Django in the Python shell.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...