Django: QuerySet Method

Seoyul Kim·2020년 5월 17일
0

Django

목록 보기
9/12
post-custom-banner

queryset을 return하는 method

1.filter()

question.objects.filter(id=1)

2.exclude()

Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello')

3.annotate()

 q = Blog.objects.annotate(number_of_entries=Count('entry'))

4.order_by()

Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline')
Entry.objects.order_by('?')             #order randomly
Entry.objects.order_by('blog__id')
Entry.objects.order_by(Coalesce('summary', 'headline').desc())

5.reverse()

my_queryset.reverse()[:5]

6.distinct()

Entry.objects.order_by('author', 'pub_date').distinct('author')

7.values()

Blog.objects.filter(name__startswith='Beatles').values()

8.values_list()

>>>Entry.objects.values_list('id', 'headline')
<QuerySet [(1, 'First entry'), ...]>

9.dates()

10.datetimes()

11.none()

Entry.objects.none()

12.union()

qs1 = Author.objects.values_list('name')
qs2 = Entry.objects.values_list('headline')
qs1.union(qs2).order_by('name')

13.intersection()

>>> qs1.intersection(qs2, qs3)

14.difference()

>>> qs1.difference(qs2, qs3)

15.extra()

  • select
  • where / tables
  • order_by
  • params

16.defer()

Entry.objects.defer("body").filter(rating=5).defer("headline")

17.only()

  • defer() 와 반대
Person.objects.defer("age", "biography")
Person.objects.only("name")

18.using()

Entry.objects.using('backup')

19.select_for_update()


queryset을 return하지 않는 method

get()

  • 쿼리셋이 아니라 python 객체 형태를 반환하기 때문에 dot nation으로 접근할 수 있다.
User.objects.get(id=1).name
  1. create()

  2. get_or_create()

  • 튜플 (object, created) 를 return한다.
obj, created = Person.objects.get_or_create(
    first_name='John',
    last_name='Lennon',
    defaults={'birthday': date(1940, 10, 9)},
)
  1. update_or_create

  2. count()

6.iterator()

7.latest()

8.earliest()

9.first()

10.last()

11.aggregate()

q = Blog.objects.aggregate(number_of_entries=Count('entry'))

12.exists()

13.update()

14.delete()

15.explain()


Field lookups

  • SQL의 WHERE문을 지정할 때 사용된다.

1.extact

Entry.objects.get(id__exact=14)
Entry.objects.get(id__exact=None)

SELECT ... WHERE id = 14;
SELECT ... WHERE id IS NULL;

2.iexact

#'Beatles Blog', 'beatles blog', 'BeAtLes BLoG 와 매치된다.
Blog.objects.get(name__iexact='beatles blog')  

Blog.objects.get(name__iexact=None)

SELECT ... WHERE name ILIKE 'beatles blog';
SELECT ... WHERE name IS NULL;

3.contains

Entry.objects.get(headline__contains='Lennon')

SELECT ... WHERE headline LIKE '%Lennon%';

4.icontains

Entry.objects.get(headline__icontains='Lennon')

SELECT ... WHERE headline ILIKE '%Lennon%';

5.in

inner_qs = Blog.objects.filter(name__contains='Cheddar')
entries = Entry.objects.filter(blog__in=inner_qs)

SELECT ... WHERE blog.id IN (SELECT id FROM ... WHERE NAME LIKE '%Cheddar%')

6.gt(greater than)

Entry.objects.filter(id__gt=4)

7.gte(greater than equal)

8.lt(less than)

9.lte(less than equal)

10.startswith

Entry.objects.filter(headline__startswith='Lennon')

11.istartswith

Entry.objects.filter(headline__istartswith='Lennon')

12.endswith

Entry.objects.filter(headline__endswith='Lennon')

13.iendswith

Entry.objects.filter(headline__iendswith='Lennon')

14.range

import datetime
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date))

15.date / year /month / day / week / week_day

Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))

Entry.objects.filter(pub_date__year=2005)
Entry.objects.filter(pub_date__year__gte=2005)

Entry.objects.filter(pub_date__month=12)
Entry.objects.filter(pub_date__month__gte=6)

Entry.objects.filter(pub_date__day=3)
Entry.objects.filter(pub_date__day__gte=3)

Entry.objects.filter(pub_date__week=52)
Entry.objects.filter(pub_date__week__gte=32, pub_date__week__lte=38)

Entry.objects.filter(pub_date__week_day=2)
Entry.objects.filter(pub_date__week_day__gte=2)

Aggregation 함수

1.expressions

2.output_field

3.filter

4.Avg

5.Count

6.Max

7.Min

8.Sum

post-custom-banner

0개의 댓글