我正在努力解决与django自定义管理命令相关的问题。这是用于从json文件加载数据的自定义管理命令代码:
import time
import json
import sys
from cycu.models import hfModel
from django.db import IntegrityError
from django.core.management import BaseCommand
from django.utils.text import slugify
from django.contrib.gis.geos import Point
class Command(BaseCommand):
stdin = sys.stdin
def handle(self, *args, **options):
start_time = time.time()
self.stdout.write("Loading Facilities...")
data = json.load(self.stdin)
if data:
hfModel.objects.all().delete()
instances = []
for d in data["features"]:
try:
if d["properties"]["statename"] == 'Kano':
a = hfModel(
id=int(d["properties"]["id"]),
wardname=d["properties"]["wardname"],
wardcode=d["properties"]["wardcode"],
lganame=d["properties"]["lganame"],
lgacode=int(d["properties"]["lgacode"]),
zonename=d["properties"]["zone"],
statename=d["properties"]["statename"],
source=d["properties"]["source"],
ownership=d["properties"]["ownership"],
category=d["properties"]["category"],
primary_name=d["properties"]["primary_name"],
hthfa_code=d["properties"]["hthfa_code"],
hthfa_code_slug=slugify(d["properties"]["hthfa_code"]),
masterlist_type=d["properties"]["masterlist_type"],
point=Point(d["geometry"]["coordinates"])
)
instances.append(a)
except …Run Code Online (Sandbox Code Playgroud)