본문 바로가기

Django22

django fliter 에다 list 넣기 ids = [1, 3, 6, 7, 9] for id in ids: MyModel.objects.filter( pk=id )이런식으로 리스트의 값들만 filter 에 넣어서 가지고 오고 싶을때. # Get blogs entries with id 1, 4 and 7 >>> Blog.objects.filter(pk__in=[1,4,7]) # Get all blog entries with id > 14 >>> Blog.objects.filter(pk__gt=14)__in 을 통해서 가능하다. 출처 : https://stackoverflow.com/questions/9304908/django-filter-with-list-of-values?utm_medium=organic&utm_source=google_rich.. 2018. 3. 29.
django query filter, value, distinct 1. filter 만 했을 때와 , filter().values() 했을 때 차이. # This list contains a Blog object. >>> Blog.objects.filter(name__startswith='Beatles') # This list contains a dictionary. >>> Blog.objects.filter(name__startswith='Beatles').values() 원하는 속성값만 가지고 오고 싶을 땐, 매개변수로 속성값을 넣어서 가져온다.>>> Blog.objects.values() >>> Blog.objects.values('id', 'name') 2. distinct()¶distinct(*fields)¶ SQL 문에서 SELECT DISTINCT 한 것과.. 2018. 3. 28.
django query Django에서 쿼리셋 효과적으로 사용하기 http://raccoonyy.github.io/using-django-querysets-effectively-translate/ tree_set = Tree.objects.filter(type="deciduous") # exists()는 쿼리셋 캐시를 만들지 않으면서 레코드가 존재하는지 검사한다 if tree_set.exists(): # DB에서 가져온 레코드가 하나도 없다면 # 트래픽과 메모리를 절약할 수 있다 print("There are still hardwood trees in the world!")star_set = Star.objects.all() # iterator() 메서드는 전체 레코드의 일부씩만 DB에서 가져오므로 # 메모리를 절약할 수 있다.. 2018. 3. 28.
[django] ajax 사용해서 화면 수정 간단한 웹 프로젝트를 하고 있는데 페이지 갱신없이 화면의 일부를 바꿔줄 일이 필요해졌다.ajax를 사용해서 비동기적인 처리를 해주었다. Ajax(Asynchronous JavaScript and XML, 에이잭스) Ajax는 웹브라우저와 웹서버가 내부적으로 데이터 통신을 하게 된다. 그리고 변경된 결과를 웹페이지에 프로그래밍적으로 반영함으로써 웹페이지의 로딩 없이 서비스를 사용할 수 있게 한다. 자바스크립트를 이용해서 비동기적으로 서버와 브라우저가 데이터를 주고 받는 방식을 의미한다. java로 웹개발할 때 많이 사용했었는데django라고해서 크게 다르지 않아서 어려움은 없었다. > 화면 template/index.htmlsearch_button 을 클릭하면 search_input 창에 입력한 검색어를 .. 2018. 3. 19.