Three methods
Product(values);
Product.objects.create(name="Nexus" , price=10534)
Product.objects.create(**dict_obj)
Product.objects.filter(id=2).update(name="Kia Seltos II")
car = Product.objects.get(id=2)
car.name = 'Kia Seltos'
Product.objects.filter(id=2).delete()
-
OrderBy: Recipe.objects.all().order_by('-view_count ...: ') For Decending Order
-
OrderBy: Recipe.objects.all().order_by('view_count ...: ') For Ascending Order
-
vege = Recipe.objects.all().order_by('view_count' ...: )[0:1] (Here
[0:1]
tells that we limit the response and we want only 1 item) -
Recipe.objects.filter(view_count__gte=55) : Here gte means greater and equal to
- Here the
dept
is the foreign key anddepartment
is the attribute of the modelDepartment
. Here is to add the filters dept__department__icontains
- It is used to exclude the objects from the queryset.
In [41]: query = Student.objects.aggregate(Avg('student_age'))
In [42]: query
Out[42]: {'student_age__avg': 25.387096774193548}
In [43]: Student.objects.aggregate(Max('student_age'))
Out[43]: {'student_age__max': 30}
In [44]: Student.objects.aggregate(Min('student_age'))
Out[44]: {'student_age__min': 18}
In [45]: Student.objects.aggregate(Sum('student_age'))
Out[45]: {'student_age__sum': 787}