- 쟝고
- 이진트리
- Article & User
- 통계학
- outer join
- ORM
- Django
- update
- SQL
- 백트래킹
- N:1
- stack
- Queue
- 스택
- M:N
- drf
- 큐
- distinct
- regexp
- 그리디
- DB
- delete
- 트리
- create
- 완전검색
- migrations
- Tree
- count
- Vue
- 뷰
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
목록쟝고 (44)
데이터 분석 기술 블로그
Article(N) - User(1) 0개 이상의 게시글은 1명의 회원에 의해 작성될 수 있습니다. Comment(N) - User(1) 0개 이상의 댓글은 1명의 회원에 의해 작성될 수 있습니다. 1. Article & User 1-1 모델 관계 설정 User 외래 키 정의 # articles/models.py from django.conf import settings class Article(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) title = models.CharField(max_length=10) content = models.TextField() created_at = m..
1. 댓글 READ 구현 detail view 함수에서 전체 댓글 데이터를 조회 # articles/views.py from .models import Article, Comment def detail(request, pk): article = Article.objects.get(pk=pk) comment_form = CommentForm() comments = article.comment_set.all() context = { 'article': article, 'comment_form': comment_form, 'comments': comments, } return render(request, 'articles/detail.html', context) 댓글 목록 {% for comment in co..
1. 사용자로부터 댓글 데이터를 입력받기 위한 CommentForm 정의 # articles/forms.py from .models import Article, Comment class CommentForm(forms.ModelForm): class Meta: model = Comment fields = '__all__' 2. deatil view 함수에서 CommentForm을 사용하여 detail 페이지에 렌더링 # articles/views.py from .forms import ArticleForm, CommentForm def detail(request, pk): article = Article.objects.get(pk=pk) comment_form = CommentForm() context ..
1. 역참조 역참조란, N:1 관계에서 1에서 N을 참조하거나 조회하는 것입니다. 즉, 1 → N입니다. N은 외래 키를 가지고 있어서 물리적으로 참조가 가능하지만 1은 N에 대한 참조 방법이 존재하지 않기 때문에 별도의 역참조 이름이 필요합니다. 2. related manager N:1 혹은 M:N 관계에서 역참조 시에 사용하는 매니저입니다. 'objects' 매니저를 통해 queryset api를 사용했던 것처럼 related manager를 통해 queryset api를 사용할 수 있게 됩니다. 3. Related manager 연습 shell_plus 실행 및 1번 게시글 조회 python manage.py shell_plus article = Article.objects.get(pk=1) 1번 ..
1. shell_plus 실행 및 게시글 작성 python manage.py shell_plus # 게시글 생성 Article.objects.create(title='title', content='content') 2. 댓글 생성 # Comment 클래스의 인스턴스 comment 생성 comment = Comment() # 인스턴스 변수 저장 comment.content = 'first comment' # DB에 댓글 저장 comment.save() # 에러 발생 django.db.utils.IntegrityError: NOT NULL constraint failed: articles_comment.article_id # articles_comment 테이블의 ForeignKeyField, article..
1. 댓글 모델 정의 ForeignKey() 클래스의 인스턴스 이름은 참조하는 모델 클래스 이름의 단수형으로 작성하는 것을 권장합니다. ForiegnKey 클래스를 작성하는 위치와 관계없이 외래 키는 테이블 필드 마지막에 생성됩니다. # articles/models.py class Comment(models.Model): article = models.ForeignKey(Article, on_delete=models.CASCADE) content = models.CharField(max_length=200) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(atuo_now=True) 2. Forign..
1. Template with Authentication data 템플릿에서 인증 관련 데이터를 출력하는 방입니다. 2. 현재 로그인되어있는 유저 정보 출력하기 Hello, {{ user.username }} 3. context processors 템플릿이 렌더링 될 때 호출 가능한 콘텍스트 데이터 목록입니다. 작성된 컨텍스트 데이터는 기본적으로 템플릿에서 사용 가능한 변수로 포함됩니다. 즉, django에서 자주 사용하는 데이터 목록을 미리 템플릿에 로드해 둔 것입니다.
Logout Logout이란 Session을 Delete 하는 과정입니다. logout(request) 현재 요청에 대한 Session Data를 DB에서 삭제하고 클라이언트의 쿠키에서도 Session Id를 삭제합니다. 1. 로그아웃 로직 작성 # accounts/urls.py urlpatterns = [ path('login/', views.login, name='login'), path('logout/', views.logout, name='logout'), ] # accounts/views.py from django.contrib.auth import logout as auth_logout def logout(request): auth_logout(request) return redirect('a..