Django ORM queries- All Filter Property in single article

Create a model first and then you can continue with following queries, we have created model which surely helps you to understand in better ways.

from django.db import models

class Blog(models.Model):
    headline = models.CharField(max_length=255)
    tag = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField(
    number_of_comments = models.IntegerField(default=0)
    number_of_pingbacks = models.IntegerField(default=0)
    rating = models.IntegerField(default=5)

    def __str__(self):
        return self.headline

Filtering the Records With name starts with s

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 headline starts with the S.

queryset = Blog.objects.filter(headline__startswith = 'S')  
>>> queryset  
<QuerySet [<Blog: Save earth>, <Blog: Safe measurement>]>  
>>> str(queryset.query)  

Here we will fetch the data which headline start with R and tag start with S

queryset = Blog.objects.filter(headline__startswith = 'R') | Blog.objects.filter(tag__startswith = 'S')