#Django post_save
Explore tagged Tumblr posts
Text
Effective Django Signals: Mastering Connection and Listening
Mastering Django Signals: Connecting and Listening Effectively
Introduction Django signals are a powerful feature that allows decoupled applications to get notified when certain actions occur elsewhere in the framework. They provide a way for a piece of code to send notifications to interested parties without requiring those parties to be tightly coupled. This article will delve into the world of Django signals, explaining how to connect and listen to them…
#decoupled applications#Django custom signals#Django post_save#Django receiver#Django signals#Python web development
0 notes
Text
django rest framework api
https://youtu.be/44qdTGbWY8c
아래 작업전 virtualenv를 이용해서 작업 공간을 만든다.
pip 으로 설치한 디펜던시 requirements.txt에 저장하기
.
.
2강 https://youtu.be/uzO2PW5jNMk
서버에서의 객체를 외부로 보낼때 serializing 작업을 해서 내보내고
서버로 들어온 데이터를 deserializing을 해서 객체로 만들어서 작업한다.
serializer는 두가지가 있으며
아래는 일반 serializer를 보여주고 있다.
아래는 위 보다는 간단한 model serializer를 보여주고 있다.
rest framework를 사용하는 경우 폴더를 따로 만들어 관리하는 것이 좋다.
이 예제에서는 api 폴더를 만들었고 모든 python 프로젝트 내의 폴더는 __init__.py를 가지고 있어야 python이 폴더로 인식을 한다.
.
.
3강
rest_framework를 이용한 view의 형식
rest framework 를 위한 app urls
전체 urls에 app urls을 추가하는 경우
.
.
4강
put , delete, post 처리 view 예시를 보여주고 있다.
serializer를 이용해서 update하기
https://stackoverflow.com/questions/69071531/how-to-use-django-serializer-to-update-an-instance
put 을 이용해서 update한다.
dictionary 형태로 데이터를 Response()함수에 넣어주면 json 형태로 클라이언트에게 전달된다.
(참고) serializer partial update
https://www.django-rest-framework.org/api-guide/serializers/#partial-updates
.
.
6강
https://www.django-rest-framework.org/api-guide/serializers/#additional-keyword-arguments
sign up , registration, register
serializer의 save()를 오버라이드해서 저장될때 작업을 추가 할수 있다.
https://www.django-rest-framework.org/api-guide/serializers/#additional-keyword-arguments
위에서는 extra_kwargs를 사용해서 password는 입력받아 obj를 생성하지만 저장되어있는 obj를 사용자에게 보일때는 password는 보이지 않게 한다.
.
.
7강
authentication token
https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
아래는
settings, post_save_receiver 그리고
rest_framework에서 Token 모델을 가져온 모습
사용자 user account 가 만들어 질때 마다 receiver가 작동해서 아래 코드를 수행한다.
여기 예제에서는 매번 사용자가 만들어질때마다 token이 만들어져 저장된다.
post_save : https://docs.djangoproject.com/en/3.0/ref/signals/#post-save
.
.
8강
authentication과 permission을 조합해서 사용하는 경우
이 예제 프로젝트에서는 token authentication으로 확���된 사용자가 특정 페이지 작업을 할수 있게 하는 것을 보여준다.
아래는 permission작업을 authentication을 이용해서 한다는 내용
아래는 decorators permission_classes, rest_framework.permissions에서 IsAuthenticated를 가져온것을 보여준다.
아래의 경우 django가 알아서 token을 통해 서버내에서 사용자를 찾아서 request.user에 넣어준다.
.
.
9강
Serializer에 field를 추가 하는 경우
아래는 BlogPost model을 기반으로한 serializer에다가 username field를 추가하는 경우이다. get_username_from_author 함수를 이용해서 username 값을 만들수 있다.
.
.
10강
pagination
settings.py에 REST_FRAMEWORK 내용을 추가해준다.
views.py에서 필요한 라이브러리 import한다.
class view를 이용한 경우를 보여주고 있다.
class view를 urls에 추가하는 경우 as_view()를 이용한다
아래는 결과모습이다. 아래와 같이 json 형태로 각 페이지에 필요한 데이터를 내보낸다.
0 notes
Link
One of the benefits of the app structure of Django is that disparate pieces of your project don’t necessarily know about each other. But sometimes something happening in one app needs to trigger an action in another. Signals to the rescue. Some signals, such as post_save, come built in. via Pocket
0 notes