from django.contrib import admin
from .models import Post
admin.site.register(Post)

그럼 위와 같이 post라는 모델이 admin에 등록이 된다.

앞서 만들었던 post 모델에 맞게 폼이 형성이 되는데,
create와 update에 해당하는 시간이 보이지 않는 이유는 auto_now라는 옵션 때문이다. 이 옵션은 시각이 자동으로 입력되는 부분이라서 폼에는 노출이 되지 않는다.

만약 글을 작성하고 history를 보면 위와 같이 수정하고 생성한 시각이 뜬다.
만약 내가 수정한 시각이나 생성한 시각 등 다른 정보를 보고 싶을 땐
list_display라는 리스트를 이용하면 된다.
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ['id', 'message', 'created_at', 'updated_at']

(venv01) (base) djangoPR % python3 manage.py shell
Python 3.8.8 (default, Apr 13 2021, 12:59:45)
[Clang 10.0.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
from instagram.models import Post
Post.objects.all()
<QuerySet [<Post: fff>]>
Post.object.all().filter(message_icontains='f')
Traceback (most recent call last):
File "", line 1, in
AttributeError: type object 'Post' has no attribute 'object'
Post.objects.all().filter(messageicontains='f')
<QuerySet [<Post: fff>]>
ps = Post.objects.all().filter(messageicontains='f')
print(ps.query)
SELECT "instagram_post"."id", "instagram_post"."message", "instagram_post"."created_at", "instagram_post"."updated_at" FROM "instagram_post" WHERE "instagram_post"."message" LIKE %f% ESCAPE '\'**
db로 전달되는 query를 확인할 수 있다.
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ['id', 'message', 'created_at', 'updated_at', 'message_length']
list_display_links = ['message']
search_fields = ['message']
또한 마지막 코드를 통해서 admin 화면 상에서도 필터링이 가능하다.
즉 message에 대해서만 필터링을 한다는 의미임.

이렇게 검색창이 구현이 된다.
지정 필드값으로 필터링 옵션이 제공된다.

오른쪽 상단에 보면 created_at을 기준으로 한 필터 기능이 제공된다.