Django Querysets Simple queries on a standalone model

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Here is a simple model that we will use to run a few test queries:

class MyModel(models.Model):
    name = models.CharField(max_length=10)
    model_num = models.IntegerField()
    flag = models.NullBooleanField(default=False)

Get a single model object where the id/pk is 4:
(If there are no items with the id of 4 or there are more than one, this will throw an exception.)

MyModel.objects.get(pk=4)

All model objects:

MyModel.objects.all()

Model objects that have flag set to True:

MyModel.objects.filter(flag=True)

Model objects with a model_num greater than 25:

MyModel.objects.filter(model_num__gt=25)

Model objects with the name of "Cheap Item" and flag set to False:

MyModel.objects.filter(name="Cheap Item", flag=False)

Models simple search name for specific string(Case-sensitive):

MyModel.objects.filter(name__contains="ch")

Models simple search name for specific string(Case-insensitive):

MyModel.objects.filter(name__icontains="ch")


Got any Django Question?