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")