fix: 위치 데이터가 수정 페이지에 보이지 않고 변동없을 경우 반영이 안되는 이슈 해결#148
Merged
Conversation
수정 페이지 진입 시, 위경도 데이터를 가상 필드인 geo에 넣어서 보이게 설정 모델 변경 시, geo에 있는 위경ㄷ 데이터를 뽑아 DB에 저장하는 방식으로 진행 추가로 코스 구간에 대해 print를 할 경우 PlaceID가 아닌 순수 구간 이름으로 나타낼 수 있게 수정
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 주요 변경 내용 (Description)
문제 상황
PlaceAdmin수정 페이지(/admin/place/edit/{id})에서 다음과 같은 문제가 발생했습니다.geom/geog값이 실제로 존재함에도 불구하고, 수정 페이지의 위경도 입력 필드(geo)는 비어 있는 상태로 렌더링됨.geom이NULL이 되어 저장에 실패함.동일한 패턴을 사용하는
KmaMountainForecastAreaAdmin도 같은 문제를 가지고 있었습니다.원인 분석
1. 수정 페이지에서 기존 위경도가 표시되지 않은 이유
PlaceAdmin은 실제 컬럼geom/geog을form_excluded_columns로 제외하고,scaffold_form에서 가상 필드geo(StringField)를 동적으로 추가하는 구조입니다.sqladmin은 수정 페이지 렌더링 시
Form(obj=model, data=...)형태로 wtforms 폼을 생성하는데(sqladmin/application.py:578), wtforms는 각 필드를getattr(obj, field_name)으로 채웁니다. 그런데model(Place 인스턴스)에는geo라는 속성이 존재하지 않으므로 필드는 항상 빈 값으로 렌더링되었습니다.2. 위경도 변동 없이 저장할 때 에러가 발생한 이유
기존
on_model_change구현은 다음과 같았습니다.geo가 비어 있는 경우 data.pop("geo", None)을 하지 않고 조기 return 하기 때문에, data 딕셔너리에는 여전히 {"geo": ""}가 남은 채로 sqladmin 내부 로직으로 흘러 들어갑니다.
sqladmin의 _set_attributes_async(sqladmin/_queries.py:107)는 다음과 같이 동작합니다.
geo는 SQLAlchemy 매퍼에 등록된 실제 컬럼이 아니므로 column은 None이고, 빈 문자열이 들어와 조건 분기에서 column.nullable에 접근하면서 문제가 됩니다. 결과적으로 실제 NOT NULL 컬럼인 geom/geog은 form에서 제외되어 있어 갱신되지 않은 채, 저장 경로가 꼬여 최종적으로 geom이 NULL인 상태로 커밋이 시도되어 에러가 발생했습니다.
수정 내용
sqladmin이 수정 페이지용 모델을 로드하는 get_object_for_edit를 오버라이드하여, 기존 geom 값을 "lat, lng" 문자열 형태로 obj.geo에 주입합니다. 이렇게 하면 이후 Form(obj=model)에서 wtforms가 getattr(obj, "geo")를 호출할 때 현재 위경도가 자연스럽게 채워집니다.
가상 필드 geo가 sqladmin의 setattr 루프에 절대 도달하지 못하도록, 조건 분기 이전에 항상 data.pop("geo", None)을 수행하도록 변경했습니다. 값이 비어 있으면서 수정(edit) 모드인 경우에는 기존 geom/geog 값을 그대로 유지하도록 early return 하고, 생성(create) 모드인 경우에는 명확한 400 에러를 반환합니다.
PlaceAdmin과 완전히 동일한 구조(geom/geog 제외 + 가상 geo 필드)를 갖고 있어 같은 버그가 잠재되어 있었으므로, 두 어드민 클래스에 모두 동일한 수정을 적용했습니다.
동작 확인
🔗 관련 이슈 (Related Issues)