Я пишу скрипт для наполнения таблиц базы данных. Мне нужно получить внешний ключ из таблицы City. В таблицы City 3 поля: id, name, reference. Внешний ключ мне нужен для таблицы Warehouses с полями id, name,reference, city_id ( внешний ключ ). Все данные я беру из API.
def refresh_warehouses():
api_domain = 'https://api.novaposhta.ua'
api_path = '/v2.0/json/Address/getWarehouses'
api_data = {
"modelName": "Address",
"calledMethod": "getWarehouses",
"methodProperties": {
"Limit": "5"
},
'apiKey': settings.NOVA_POSHTA_API_KEY
}
response = requests.post(api_domain + api_path, json=api_data).json()
if not response.get('success'):
raise Exception(','.join(response.get('errors')))
Warehouse.objects.all().delete()
warehouses = []
for item in response.get('data'):
params = {
'name': item.get('Description'),
'reference': item.get('Ref'),
'city_id': City.objects.get(name=item.get("CityDescription")).id,
'delivery_method_id': 1
}
warehouses.append(Warehouse(**params))
Warehouse.objects.bulk_create(warehouses)
После выполнения скрипта django выдает ошибку FOREIGN KEY constraint failed
.
Подскажите как лучше взять id из таблицы City?