Vous êtes sur la page 1sur 3

DJANGO MODEL FUNCTIONS

NAME DEFENITION EXAMPLE SQL EQUIVALENT

all() Returns whole data from Products.objects.all() Select * from Products


a given object

get() Returns a single Products.objects.get(id=1) Select * from Products where id=1


object. Throws an
error if lookup
returns multiple
objects
filter() Filter by the given Products.objects.filter(id<=10) Select * from Products where id<=10
lookup parameters
and returns a queryset

order_by() Sorts the data by a given Products.objects.all().order_by(‘Name’). Select * from Products order by Name
column or a list of asc()
columns
Or
Or
Products.objects.all().order_by(‘Name’).
Select * from Products order by Name
asc()
desc

exclude() Filter by objects that Products.objects.all().exclude(name=’No Select * from Products where not
don’t match the given kia’) name=’Nokia’
lookup parameters

distinct() Perform an Products.objects.distinct(‘Name’) Select distinct name from Products


SQL SELECT
DISTINCT query to
eliminate duplicate
rows
values() Returns Products.objects.filter(name=’Hp’).value
a QuerySet that s
returns dictionaries,
rather than model
instances, when used as
an iterable.

values_list() Returns tuples


instead of model
instances
DJANGO MODEL FUNCTIONS

dates() Returns a QuerySet


containing all
available dates in the
specified date range

raw()d Execute a raw SQL Method Description


statement

Aggregate() Return a dictionary of Products.objects.aggregate(Cou Select count(productid) from


aggregate values nt(‘productid’)) Products
calculated over the
QuerySet
Applies the applied Products.objects.values(‘name’ Select name,sum(price) from
annotate()
function to each row ).annotate(Sum(‘price’) Products group by name
or object instead of
retrieving summary
value
select_related() Performs joining Products.objects.select_related( Select * from products inner join
querysets .It doesnt ‘productimage’) productimage on
work with many to #it displays the data from a table products.pk=productimage.fk
many relationship named productimage
create() Creates a record in Products.objects.create(name=’ Insert into products(name,price)
the table ’,price=’’) values(‘’,’’)

delete() Deletes a record Products.objects.get(id=1).delet Delete from products where id=1


e()

count() Returns the count of Products.objects.filter(name=’ Select count(*) from Products where
objects Hp’).count() name=’Hp’
DJANGO MODEL FUNCTIONS

contains/icont Field contains search Products.objects.filter(name__c Select * from products where name
ains text. icontains is ontains=’Hp’) like ‘Hp’
case-insensitive
version
in Returns rows that Products.objects.filter(name__i Select * from products where name
matches a list n=[‘nokia’,’samsung’,’hp’]) in(‘nokia’,’samsung’,’hp’)

Q operator:

Is used when the query is more comples.if you want to use OR operator in queries filter alone doesnt
work. You can use Q operator to implement this functionality,

Ex: from django.db.models import Q

Products.objects.get(Q(name__startswith=’N’) | Q(name__startswith=’H’))

Vous aimerez peut-être aussi