Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Priority Queue
- JavaScript
- Back tracking
- Vue.js
- 알고리듬
- 시뮬레이션
- Python
- 구현
- Data Structure
- SQL
- SWEA
- hash table
- GitHub
- BFS
- programmers
- CSV
- aws
- 코테
- 모의SW역량테스트
- 알고리즘
- Linked list
- Bruth Force
- spring boot
- gpdb
- boj
- django
- 코딩테스트
- DFS
- Trie
- Algorithm
Archives
- Today
- Total
hotamul의 개발 이야기
[hotamul] Django Admin Action without selecting objects 본문
project/share-blog
[hotamul] Django Admin Action without selecting objects
hotamul 2022. 10. 10. 23:35Django Admin에서 Item을 선택하지 않고 Go(Aciton) 버튼을 클릭하면 위와 같이 Item이 선택되어야 한다는 경고 메시지를 볼 수 있다.
share-blog에 블로그들의 (velog - giruboy, tistory - hotamul) 최근 게시물을 가져오는 기능을 Django Admin Action을 이용해 구현하려고 하기 때문에 해당 Error가 발생하지 않도록 해야한다.
해결 방법
- action method 추가
# blog/admin.py ... @admin.register(Post) class PostAdmin(admin.ModelAdmin): list_display = ( 'id', 'category', ... actions = ['get_the_latest_blog_posts', ] def get_the_latest_blog_posts(self, request, queryset): cnt = len(queryset) crawlers = create_crawlers() for c in crawlers: c.crawl() cnt = Post.objects.all().count() - cnt if cnt: msg = '{} posts were created successfully.'.format(cnt) else: msg = 'It is already up to date.' self.message_user(request, msg, messages.SUCCESS) ...
changelist_view
overriding
# blog/admin.py
...
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = (
'id',
'category',
...
def changelist_view(self, request, extra_context=None):
if 'action' in request.POST \
and request.POST['action'] == 'get_the_latest_blog_posts':
if not request.POST.getlist(ACTION_CHECKBOX_NAME):
post = request.POST.copy()
for u in Post.objects.all():
post.update({ACTION_CHECKBOX_NAME: str(u.id)})
request._set_post(post)
return super(PostAdmin, self).changelist_view(request, extra_context)
...
만약
request.POST
'action'
이름이'get_the_latest_blog_posts'
이고request
에 item list가 한 개도 없다면 (selected된 아이템이 없다면) 현재 모든Post
를request
에 추가한다.
Post.objects.all()
로 처리한 이유는 나중에 저장되어 있는 게시물들의 url과 크롤링을 통해 가져온 url과 비교해서 저장되어 있지 않은 url만 크롤링 하기 위해서 이다.
참고: django admin action without selecting objects - Stack Overflow
'project > share-blog' 카테고리의 다른 글
[hotamul] AWS ALB에서 HTTP 트래픽을 HTTPS로 리디렉션하려면 (0) | 2022.10.13 |
---|---|
[hotamul] json으로 Django secret key 관리 하기 (0) | 2022.09.27 |
[hotamul] 태그 제거되는 버그 수정 (0) | 2022.09.20 |
[hotamul] Django 실행 시 sqlite3 버전 이슈 (0) | 2022.09.16 |
[hotamul] EC2에서 Python3.8 설치 및 버전 변경 (0) | 2022.09.16 |
Comments