본문 바로가기

728x90
반응형

전체 글

(263)
Django ManyToMany로 생기는 중간테이블(intermediate table)에 접근하기 # through # 중간테이블 정의 없이 from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) # 새로운 저자와 책 객체 생성 book = Book.objects.create(title="Example Book") # 자동 생성된 중간 테이블에 대한 queryset 가져오기 through_queryset = book.authors.through.objects.all() .through를 통해 중간테이블 데이터를 직접 활용 가능 (join..
Python: rsplit # 파일 확장자 명 떼어서 이름 붙이기 rsplit 파일의 확장자 명과 이름 분리하기 (예시: abc.png) ChatGPT 와의 문답 내용 첨부 Q) How to remove last part of string in python? for example, abc.png -> I want to remove .png only A) You can remove the last part of a string in Python using the str.rsplit() method. In your example, you want to remove the file extension ".png" from the filename. Here's how you can do it: filename = "abc.png" base_name = filename.rspli..
Javascript: new Date # 날짜 1) 원하는 형태로 조정 const now = new Date(); // create a new Date object with the current date and time const year = now.getFullYear(); const month = (now.getMonth() + 1).toString().padStart(2, '0'); // add 1 to the month (since getMonth() returns a zero-based index) and pad with leading zero if necessary const day = now.getDate().toString().padStart(2, '0'); // pad with leading zero if necessary const..
Django 최신 데이터 가져오기# DateTimeField # latest # earliest Django에서 모델 설계시에 created나 updated field DateTimeField 형태로 추가 후에 가장 최근에 수정된 object 접근시 latest() 가장 오래 전 수정된 object 접근시 earliest() example new_lecture = Lectures.objects.latest('updated') old_lecture = Lectures.objects.earliest('updated') 주의) 해당 조건에 걸리는 data row가 여러개라면, 여러 개중에 한가지만 불러오기 때문에 전(후)처리가 필요할 수 있습니다. (id가 가장 큰 것을 가져오는 듯)
Python: moviepy # duration # 비디오 파일 재생시간 가져오기 Python moviepy library To get the total playing time of a video in Django, you can use the moviepy library, which is a powerful library for video editing in Python. First, you need to install the library using pip: pip install moviepy Then, you can use the VideoFileClip class from the moviepy.editor module to read the video file and obtain its duration: from moviepy.editor import VideoFileClip d..
Django: bulk create Djnago ORM query 중 bulk_create를 통해서 list에 class instance들을 담아서 한번에 생성할 수 있다. 한번의 쿼리로 여러 row를 생성 할수 있어서 DB에 overhead를 발생시키지 않는다. 주의할 점은, 데이터 무결성은 보장이 안되니 bulk_create 를 하려면 데이터 전처리나 검증을 확실히 해야 한다. ------------- 사용 예제 models.py from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=200) author = ..
git hooks: .git/hooks .git/hooks git에는 특정 명령어 단계에 따라서 원하는 작동을 script로 실행할 수 있는 기능이 있다. git으로 관리되는 repository에 들어가서 .git/hooks 를 살펴보면 pre-commit.sample, pre-merger-commit.sample 등등의 깃허브 명령어와 관련된 sample 파일들이 있다. 예를 들어 pre-commit 파일 script를 만들어서 실행할 수 있는 예시들이다. $ nano .git/hooks/pre-commit 를 통해 아래 스크립트를 작성해서 commit 전에 실행시킬 수도 있다. #!/bin/sh if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then against=HEAD else # Initial..
Django 도메인 설정 후에 CSRF 설정 EC2 등에 Django web application을 등록하고 도메인을 설정한 경우, csrf 문제로 403 forbidden이 뜨는 경우가 있다. CSRF_TRUSTED_ORIGINS = [ "https://mysite.com", ] 위와 같은 방식으로 settings.py에 설정을 추가해줘야 한다. ALLOWED_HOST에서 "*" 모두 허용 했던걸로 해보면, 안먹힌다. ssl 인증을 받아서 http로 접속시 https로 redirect 되는 식으로 nginx를 설정해놨다면, http://mysite.com 으로 등록할 경우에도 403 forbidden 문제가 발생. 꼭 redirect 되어지는 https:// 로 CSRF_TRUSTED_ORIGINS에 추가해야 함 mysite.com 도 안되었음

728x90
반응형