- count
- 뷰
- 완전검색
- M:N
- outer join
- DB
- 스택
- delete
- 이진트리
- N:1
- create
- 그리디
- Django
- 큐
- 쟝고
- stack
- ORM
- 통계학
- drf
- SQL
- migrations
- Article & User
- 백트래킹
- Tree
- Queue
- 트리
- distinct
- regexp
- Vue
- update
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
목록전체 글 (466)
데이터 분석 기술 블로그

1. 게시글 READ 각 게시글의 작성자 이름을 출력한다. {% for article in articles %} 작성자 : {{ article.user }} 글 번호 : {{ article.pk }} 글 제목 : {{article.title }} 글 내용: {{ article.content }} {% endfor %} 2. 게시글 UPDATE 게시글 수정 요청 사용자와 게시글 작성 사용자를 비교하여 보인의 게시글만 수정할 수 있도록 합니다. # articles/views.py @login_required def update(request, pk): article = Article.objects.get(pk=pk) if request.user == article.user: if request.method ..

게시글 CREATE 기존 Article Form 출력 변화를 확인합니다. User 모델에 대한 외래 키 데이터 입력을 위해 불필요한 input이 출력됩니다. ArticleForm 출력 필드를 수정합니다. # articles/forms.py class ArticleForm(forms.ModelForm): class Meta: model = Article fields = ('title', 'content',) 게시글 작성 시 에러 발생가 발생하면 user_id 필드 데이터가 누락되었기 때문입니다. 게시글 작성 시 작성자 정보가 함께 저장될 수 있도록 save의 commit 옵션을 활용합니다. # articles/views.py @login_required def create(request): if reque..

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..