要出家的小熊猫 · sourcetree推送到github无反应 ...· 1 年前 · |
跑龙套的啄木鸟 · java - How to ...· 1 年前 · |
温文尔雅的大象 · ICLR2023论文速递!共1573篇!都有 ...· 1 年前 · |
冷静的饼干 · MySQL中timestamp和dateti ...· 1 年前 · |
In this tutorial, we will have the complete discussion over the Django ORM queries and how we can use them to manipulate the data.
Django ORM is one of the best tools of Django and plays very essential role to perform database related tasks. It provides abstractions with the database, in a mostly database agnostic way.
Django ORM consists of ease of use abstraction. It keeps "Simple things easy and hard things possible."
Here we will have the detailed explanation of each ORM queries and also see their associated SQL queries.
First, we will create a sample database using the Django model that consists of some data and we will run the query on that database.
model.py
# Create your models here. class Student(models.Model): username = models.CharField(max_length=20) first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) mobile = models.CharField(max_length=10) email = models.EmailField() def __str__(self): return "%s %s" % (self.first_name, self.last_name)And then run the following command.
python manage.py makemigrations python manage.py migrateWe are set to run the query.
We have a model called Student. To get all records from model, we will use the Student.objects.all(). To do so, open the Django shell to run the query.
>>> from sampleapp.models import Student >>> queryset = Student.objects.all() >>> querysetYou might be wonder how Django ORM makes our queries executed or what the corresponding query of the code we are writing. It is quite simple to get the SQL query, we need to use the str() and pass the queryset object along with query.
Corresponding SQL Query
>>> str(queryset.query) 'SELECT "sampleapp_student"."id", "sampleapp_student"."username", "sampleapp_student"."first_name", "sampleapp_student"."last_name", "sampleapp_student"."mobile", "sampleapp_student"."email" FROM "sampleapp_student"'We will use the Student.objects.create() and pass the fields along with its value as argument. Let's see the below example.
>>> queryset = Student.objects.create(username = 'rahul20', first_name = 'Rahul', last_name = 'Shakya', mobile = '77777', email = 'rahul@gmail.com') >>> queryset.save()Note that, we need to use the .save() method on the query object to save the newly created record in table, otherwise it will not show in database.
Suppose we need a specific object from a queryset to matching the result. We can do this using the get() method. The get() returns the single object directly. Let's see the following example.
Example -
>>> from sampleapp.models import Student >>> queryset = Student.objects.get(pk = 1) >>> querysetExample - 2
>>> queryset = Student.objects.get(mobile = 22222) >>> querysetAs we can see in both examples, we get the single object not a queryset of a single object. If there is no result then match the query, get() will raise a DoesNotExist exception. On the other hand, if there is multiple field matches, it will raise the MultipleObjectReturned, which is an attribute of the model class itself.
In the earlier example, the QuerySet returned by all() describes the all record in the database table. But sometimes, we need to select the subset of complete set of object and it can be done by adding the filter conditions.
In the below example, we will fetch the data which first name starts with the R.
>>> queryset = Student.objects.filter(first_name__startswith = 'R') >>> queryset >>> str(queryset.query) 'SELECT "sampleapp_student"."id", "sampleapp_student"."username", "sampleapp_student"."first_name", "sampleapp_student"."last_name", "sampleapp_student"."mobile", "sampleapp_student"."email" FROM "sampleapp_student" WHERE "sampleapp_student"."first_name" LIKE R% ESCAPE \'\\\''It returns a new QuerySet containing objects that do not match the given lookup parameter. In other words, it excluded the records according the lookup condition. Let's understand the following example.
>>> queryset = Student.objects.exclude(first_name__startswith = 'R') >>> querysetOutput:
The OR operation is performed when we need the record filtering with two or more conditions. In the below example, we will get the student whose first_name starts with 'A' and last_name starts with 'M'.
Django allows us to do this in two ways.
We get the student details those first name starts with 'A' and last name starts with 'S'.
Let's the SQL query of corresponding OR operator.
>>> str(queryset.query) 'SELECT "sampleapp_student"."id", "sampleapp_student"."username", "sampleapp_student"."first_name", "sampleapp_student"."last_name", "sampleapp_student"."mobile", "sampleapp_student"."email" FROM "sampleapp_student" WHERE ("sampleapp_student"."first_name" LIKE R% ESCAPE \'\\\' OR "sampleapp_student"."last_name" LIKE S% ESCAPE \'\\\')'The AND operation is performed when we need the record matching with two or more conditions. In the below example, we will get the student whose first_name starts with 'P' and last_name starts with 'S'.
Django allows us to do this in three ways.
Only one object satisfied the given conditions.
We can also use the following query.
queryset2 = User.objects.filter( first_name__startswith='A', last_name__startswith='S' ) queryset3 = User.objects.filter(Q(first_name__startswith='R') & Q(last_name__startswith='D') )All queries will give the same result.
Sometimes we want create multiple objects in one shot. Suppose we want to create new objects at once and we don't want to run the multiple queries to the database. Django ORM provides the bulk_create to create multiple objects in one way.
>>> Student.objects.all().count()Let's create the multiple records in one query.
Student.objects.bulk_create([Student(first_name = 'Jai', last_name = 'Shah', mobile = '88888', email = 'shah@reddif.com'),Student(first_name = 'Tarak', last_name = 'Mehta', mobile = '9999', email = 'tarak@reddif.com'), Student(first_name = 'SuryaKumar', last_name = 'Yadav', mobile = '00000', email = 'yadav@reddif.com')]) [Now, our database table will update. The bulk_create takes a list of unsaved objects.
>>> Student.objects.all().count()We can set the limit on the queryset using the Python list's slicing syntax. This is equivalent operation of SQL's LIMIT and OFFSET clauses. Let's see the following query.
>>> Student.objects.all()[:4]Below query will return first record to fifth record.
>>> Student.objects.all()[1:6]Negative indexing is not supported. However, we can use the step in QuerySets.
>>> Student.objects.all()[:10:2] [To fetching the single record, we can do the following operation.
>>> Student.objects.all()[0]Django provides the order_by method for ordering the queryset. This method takes the field name which we want to Order (ascending and descending) the result. Let's see the following example.
Example - Ascending order
>>> from sampleapp.models import Student >>> Student.objects.all().order_by('mobile')For descending order, we will use the Not '-' before the query field.
>>> from sampleapp.models import Student >>> Student.objects.all().order_by('-mobile')We can also pass the multiple fields in the order_by function.
>>> Student.objects.all().order_by('first_name','-mobile')Now, we will learn how we can order the data in the relation model. We create another model called Teacher which is a related model of Student model.
Models
class Teacher(models.Model): teacher_name = models.CharField(max_length=200) def __str__(self): return f'{self.teacher_name}' class Student(models.Model): username = models.CharField(max_length=20) first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) mobile = models.CharField(max_length=10) email = models.EmailField() teacher_name = models.ForeignKey(Teacher, blank = True, null = True, on_delete= models.CASCADE)We have added teachers name and each teacher is associated with the student. Now we want to order Student by teacher_name inside each teacher_name by the Student. We can do as follows.
>>> Student.objects.all().order_by('teacher__id', 'first_name')Query field lookups are nothing but a condition which specifies same as the SQL WHERE clause. They are stated as keyword arguments to the QuerySet methods such as filter(), exclude() , and get().
Example -
Student.objects.filter(first_name__startswith = 'Ritesh')This is same as the following SQL query
Select * from Student where first_name = "Ritesh"Let's understand some important lookups.
It returns the exact result according to the search.
>>> Student.objects.get(first_name__exact = 'Arpita')Lookup should be used after the __ double underscore. We can use the case-insensitive version called iexact.
It is used to case-sensitive test. Let's see the following example.
>>> from sampleapp.models import Student >>> Student.objects.filter(last_name__contains = 'Shar')If we translate the SQL query then it will look like as below.
SELECT ... WHERE last_name LIKE '%Shar%';There is also case-incentive version called icontains.
The SQL join combines data or rows from two or more tables based on a common field between them. We can perform join operation in many ways. Let's understand the following example.
>>> q = Student.objects.select_related('teacher')Django ORM provides the grouping facility using the aggregation functions like Max, Min, Avg, and Sum . Sometimes we need to get the aggregate values from the objects. Let's understand the following example.
>>> from django.db.models import Avg, Max, Min, Sum, Count >>> Student.objects.all().aggregate(Avg('id')) {'id__avg': 5.5} >>> Student.objects.all().aggregate(Min('id')) {'id__min': 1} >>> Student.objects.all().aggregate(Max('id')) {'id__max': 10} >>> Student.objects.all().aggregate(Sum('id')) {'id__sum': 55}Truncate in SQL means clear the table data for future use. Django doesn't provide the built-in methods to truncate the table, but we can use the delete() method to get the similar result. Let's understand the following example.
>>> Student.objects.all().count() >>> Student.objects.all().delete() (10, {'sampleapp.Student': 10}) >>> Student.objects.all().count() >>> Student.objects.all()If you want to delete the individual object instance, you need to call delete() method on the individual instances of that model. We have called the delete() method on model so it deleted the entire data.
Union means getting the record which are common in both query sets. Let's see how we can do this.
>>> q1 = Student.objects.filter(id__gte = 15) >>> q2 = Student.objects.filter(id__lte = 15)In Django, we use null and blank often, by default their values are False. Both of these value work at field level where we want to keep a field null or blank. Both values seem similar but they are different in use.
If null=True means the field value is set as NULL i.e. no data. It is basically for the database column value.
date = models.DateTimeField(null=True)The blank = True specifies whether field is required in forms.
title = models.CharField(blank=True) // title can be kept blank. In the database ("") will be stored.If we set null=True blank=True, means that the field is optional in all circumstances.
teacher = models.ForeignKey(null=True, blank=True) // The exception is CharFields() and TextFields(), which in Django are never saved as ?→NULL. Blank values are stored in the DB as an empty string ('').In this tutorial, we have learned some important ORM queries. Django ORM is a powerful tool and one of the key pillars of Django. Django comes with the built-in database called SQLite. And we have described the ORM queries which acts same as the SQL queries.
Next Topic Django Form Widget