라벨이 Django인 게시물 표시

django queryset distinct on 은 표준이 아니다

  Model.objects.distinct() -> DISTINCT   : SQL 표준 Model.objects.distinct('field') -> DISTINCT ON ('field')   : PostgreSQL 전용, 다른 DB에서는 에러

Django Paginator

  Django 에서는 간단하게 페이지네이션을 구현할 수 있도록 기능이 마련되어있다. from django.core.paginator import Paginator q_page = request.GET.get('page', 1) page_obj = Paginator(object_list, 100).get_page(q_page) 이렇게 하면 설정한 크기로 잘라서 페이지를 만들어준다. (100: 한페이지에 몇줄씩 보여줄지) page_obj.object_list: 페이지로 분할한 결과 리스트 page_obj.has_other_pages: 다른페이지가 있는지 page_obj.paginator.page_range: 페이지 범위 (1~n, range 객체) page_obj.number: 이 페이지의 번호

Django 만료된 세션 삭제

  Django 는 만료된 세션을 기록을 자동으로 삭제해주지 않기 때문에 정리하지 않으면 계속 쌓인다. python manage.py clearsessions 명령으로 삭제 가능하지만, 나는 추후 있을지 모르는 분석자료를 위해 1년치는 보관하기로 했다. 삭제건수 , _ = Session . objects . filter ( expire_date__lt = 일년전 ). delete () log_msg = f '1년 이상된 session 삭제 { 삭제건수 } 건' logger . info ( log_msg )

Django 자동으로 생성된 ContentType, Permission 지우기

상황:  Django 에서 model 을 만들면, 자동으로 ContentType 과 Permission 이 생성되는데, 이후에 modeld 을 지워도 자동으로 지워지지는 않는다. 해결: shell 에서 수동으로 지운다. py manage.py shell from django.contrib.contenttypes.models import ContentType ContentType.objects.filter(app_label='your_app', model='your_model').delete() 결과: (5, {'auth.Permission': 4, 'contenttypes.ContentType': 1}) 연결된 Permission 4개도 함께 지워진다. [보완] 자동으로 지우는 커맨드도 존재한다! python manage.py remove_stale_contenttypes Some content types in your database are stale and can be deleted. Any objects that depend on these content types will also be deleted. The content types and dependent objects that would be deleted are:     - Content type for ㅇㅇㅇ.ㅇㅇㅇㅇㅇ     - 4 auth.Permission object(s) This list doesn't include any cascade deletions to data outside of Django models (uncommon). Are you sure you want to delete these content types? If you're unsure, answer 'no'. Type 'yes' to continue, or 'no' to cancel: