Django admin timeouts – 외부키 로딩 시간 길어짐

Django admin 모델을 로드하는데 ForeignKey 로 선언한 부분에서, 외부키 데이터가 엄청 많아져서 이걸 셀렉트 선택창으로 로딩하는데 시간이 소모되어 admin 창이 안뜨고 timeout으로 에러 나는 경우가 발생!!!!

해결책이 다 있다. 수정이 안되게, readonly field 로 만들어서 로딩하는 것으로 해결!

> **readonly\_fields or raw\_id\_fields **

수정도 필요하다면, 아래 글 참고

How to fix Django admin timeouts

Have you ever noticed some Django admin page taking a long time to load? Maybe even ending in a timeout (http 504)?

https://engineering.loadsmart.com/blog/how-to-fix-django-admin-timeouts

원본글 일부 발췌

Use readonly_fields

In this situation, the easiest solution would be to just set those fields as readonly_fieldsin the page definition.

class CustomAdmin(admin.ModelAdmin):
    readonly_fields = (
        "some_field", # ForeignKey with lots of instances
    )

With it, the field will be rendered on the admin page just as its current value, statically.

Must be editable?

If having a foreign key as an editable field on the admin is a must for your use case, consider these options:

Use raw_id_fields

Setting the field as a raw id field still allows its editing as just the id value of the related entity:

class CustomAdmin(admin.ModelAdmin):
    raw_id_fields = (
        "some_field", # ForeignKey with lots of instances
    )

It will render an input field on the admin with the possibility of opening a popup to search for the desired instance

Share: Twitter Facebook
Bongjun Hur's Picture

About Bongjun Hur

BJ is a dev.

Seoul, Korea https://devbj.com