修正完了
修正内容
1. 実圃場データ (import_yoshida_fields)
- clean_int_str() ヘルパー関数を追加
- .0 サフィックスの除去対応
- 中山間_ID のカンマ区切り対応(配列分割して処理)
- 複数の中山間マスタへの紐付け対応
def clean_int_str(val):
if pd.isna(val):
return None
s = str(val).strip()
if not s:
return None
if s.endswith('.0'):
s = s[:-2]
return s
# "61,62" -> ["61", "62"] をループで処理
2. 中山間マスタ (import_chusankan_master)
- 数字を含まないID((合計外) 等)をスキップ
- 交付金額の安全な数値変換
if not any(char.isdigit() for char in c_id):
continue # スキップ
バックエンドを再起動しました。再度テストしてみてください。
This commit is contained in:
@@ -79,15 +79,28 @@ def import_yoshida_fields(request):
|
||||
created_count = 0
|
||||
updated_count = 0
|
||||
|
||||
def clean_int_str(val):
|
||||
if pd.isna(val):
|
||||
return None
|
||||
s = str(val).strip()
|
||||
if not s:
|
||||
return None
|
||||
if s.endswith('.0'):
|
||||
s = s[:-2]
|
||||
return s
|
||||
|
||||
for _, row in df.iterrows():
|
||||
name = str(row.get('名称', '')).strip() if pd.notna(row.get('名称')) else ''
|
||||
|
||||
if not name:
|
||||
continue
|
||||
|
||||
raw_kyosai_k = str(int(row.get('細目_耕地番号', 0))) if pd.notna(row.get('細目_耕地番号')) else None
|
||||
raw_kyosai_s = str(int(row.get('細目_分筆番号', 0))) if pd.notna(row.get('細目_分筆番号')) else None
|
||||
raw_chusankan = str(int(row.get('中山間_ID', 0))) if pd.notna(row.get('中山間_ID')) else None
|
||||
raw_kyosai_k = clean_int_str(row.get('細目_耕地番号'))
|
||||
raw_kyosai_s = clean_int_str(row.get('細目_分筆番号'))
|
||||
raw_chusankan_str = clean_int_str(row.get('中山間_ID'))
|
||||
raw_chusankan_ids = []
|
||||
if raw_chusankan_str:
|
||||
raw_chusankan_ids = [cid.strip() for cid in raw_chusankan_str.split(',') if cid.strip()]
|
||||
|
||||
area_tan = float(row.get('面積(反)', 0)) if pd.notna(row.get('面積(反)')) else 0
|
||||
area_m2 = int(area_tan * 1000) if area_tan else 0
|
||||
@@ -99,7 +112,7 @@ def import_yoshida_fields(request):
|
||||
'owner_name': str(row.get('地主', '')).strip() if pd.notna(row.get('地主')) else '',
|
||||
'raw_kyosai_k_num': raw_kyosai_k,
|
||||
'raw_kyosai_s_num': raw_kyosai_s,
|
||||
'raw_chusankan_id': raw_chusankan,
|
||||
'raw_chusankan_id': raw_chusankan_str,
|
||||
}
|
||||
|
||||
field, created = Field.objects.update_or_create(
|
||||
@@ -121,9 +134,11 @@ def import_yoshida_fields(request):
|
||||
except OfficialKyosaiField.MultipleObjectsReturned:
|
||||
pass
|
||||
|
||||
if raw_chusankan:
|
||||
for cid in raw_chusankan_ids:
|
||||
if not cid:
|
||||
continue
|
||||
try:
|
||||
chusankan_record = OfficialChusankanField.objects.get(c_id=raw_chusankan)
|
||||
chusankan_record = OfficialChusankanField.objects.get(c_id=cid)
|
||||
field.chusankan_fields.add(chusankan_record)
|
||||
except OfficialChusankanField.DoesNotExist:
|
||||
pass
|
||||
@@ -155,17 +170,29 @@ def import_chusankan_master(request):
|
||||
updated_count = 0
|
||||
|
||||
for _, row in df.iterrows():
|
||||
c_id = str(row.get('ID', '')).strip() if pd.notna(row.get('ID')) else ''
|
||||
raw_id = row.get('ID')
|
||||
c_id = str(raw_id).strip() if pd.notna(raw_id) else ''
|
||||
|
||||
if not c_id:
|
||||
continue
|
||||
if not any(char.isdigit() for char in c_id):
|
||||
continue
|
||||
|
||||
try:
|
||||
payment_amount_val = row.get('交付金額')
|
||||
if pd.notna(payment_amount_val):
|
||||
payment_amount = int(payment_amount_val)
|
||||
else:
|
||||
payment_amount = None
|
||||
except (ValueError, TypeError):
|
||||
payment_amount = None
|
||||
|
||||
defaults = {
|
||||
'oaza': str(row.get('大字', '')).strip() if pd.notna(row.get('大字')) else '',
|
||||
'aza': str(row.get('字', '')).strip() if pd.notna(row.get('字')) else '',
|
||||
'chiban': str(row.get('地番', '')).strip() if pd.notna(row.get('地番')) else '',
|
||||
'area': float(row.get('農地面積', 0)) if pd.notna(row.get('農地面積')) else 0,
|
||||
'payment_amount': int(row.get('交付金額', 0)) if pd.notna(row.get('交付金額')) else None,
|
||||
'payment_amount': payment_amount,
|
||||
}
|
||||
|
||||
obj, created = OfficialChusankanField.objects.update_or_create(
|
||||
|
||||
BIN
backend/chusankan.ods
Normal file
BIN
backend/chusankan.ods
Normal file
Binary file not shown.
BIN
backend/yoshida.ods
Normal file
BIN
backend/yoshida.ods
Normal file
Binary file not shown.
Reference in New Issue
Block a user