Python(37)
-
Django PhoneNumberField, phonenumbers
Django에서 휴대전화 번호를 저장하는 field를 구성하는 방법은 2가지가 있다. RegexValidator을 이용해 정규식 적용 PhoneNumberField 사용 https://github.com/stefanfoulis/django-phonenumber-field RegexValidator을 이용해 정규식 적용 phone_number = models.CharField( max_length=16, blank=True, null=True, validators=[ RegexValidator( regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format '+123456789'. Up to 15 digits allowed." )..
2023.04.26 -
Django Custom Header 처리
설명 해당 문서는 Django에서 custom header를 다루기 위한 방법을 작성했습니다. 1. 요청 시 Header 형식 파스칼 케이스에서 - 를 추가한 형식으로 명명 예시 CustomHeader 라는 헤더를 추가 시: Custom-Header 2. 접근 시 Header 형식 HTTP_{your uppercased header name} 형식으로 접근 예시 Meta에서 CustomHeader 라는 헤더 조회 시: HTTP_CUSTOEM_HEADER 3. Header Permmssion 예시 class CustomHeaderPermission(BasePermission): def has_permission(self, request, view): secret_key = request.META.get('..
2023.03.08 -
Django group by 이슈
이슈사항Django는 values를 통한 group by를 해야한다. 그 와중에 annotate와 values의 순서가 중요하다. 예시아래와 같이 values 후에 annotate가 존재해야 group by 가 정상동작하는 걸 볼 수있다.Model.objects.annotate( max_value=Max(value)).values( 'group_by_value').annotate( count=Count('*') # values 아래 annotate가 있어야 group by 동작) 참고https://docs.djangoproject.com/en/4.1/topics/db/aggregation/
2023.02.11 -
[Django] order_by 시 group by 제거 방법
에러 상황 values를 통한 group by 진행 후 query는 정상적으로 동작 함. 하지만, order_by('pk') 시 group by에 pk가 추가 되어 group by 의미가 상실하는 이슈 발생 해결방법 extra를 통한order by order_by를 다른 방법으로 대체 group by로 pk 리스팅 후 sorting을 통해 재조회로 처리(query 호출이 2건이 되는 단점..)
2023.02.11 -
Pandas 엑셀 다운 시 인코딩 깨짐(utf-8, utf-8-sig)
에러 상황 엑셀 파일 생성 후 다운로드 시 한글이 깨지는 현상 발생(utf-8 형식은 동일) BOM(Byte Order Mark) 문제 https://stackoverflow.com/questions/17879198/adding-utf-8-bom-to-string-blob 해결 방법 utf-8-sig을 사용하여 encoding df.to_excel(file_path, index=False, columns=columns, header=headers, encoding='utf-8-sig') sig(signature)으로 인해 UTF-8 파일을 읽을때 BOM을 따로 처리하고 텍스트 내용과 분리하여 처리된다. 참고 https://stackoverflow.com/questions/57152985/what-is-t..
2022.11.30 -
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf9 in position 15: invalid start byte
에러 상황 pandas 로 csv 생성 후 다운로드 시 UnicodeDecodeError 발생 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf9 in position 15: invalid start byte 해결방법 Open 시 encoding 지정 file = open(path, 'rb', encoding='UTF8')
2022.11.30 -
LookupError: unknown encoding: x-windows-949
ErrorFatal Python error: init_stdio_encoding: failed to get the Python codec name of the stdio encodingPython runtime state: core initializedLookupError: unknown encoding: x-windows-949윈도우가 기본으로 949로 셋팅되어 있어서 생기는 이슈 Trouble ShootingFile > Settings > File Encodings > Project Encoding > UTF-8 위의 방법으로 해결 안될 시 확인사항가상환경 확인python 가상환경이 로컬로 되어있을 수 있음. debug 시 script path 확인
2022.11.28 -
Django request_cache 이슈 해결방법
Django 포르젝트에서 request_cache 에러 발생 시 해결방안입니다.해결 방안 프로젝트 디렉토리에서 진행cd _request_cachegit clean -dfgit checkout -- .
2022.11.07