Не получается сохранить запись в БД Postgres на Django
Мне нужно сохранить запись в БД с новым id
id_stat = await request.app.m.stat.get_one(stat_id, connection=connection)
if id_stat:
new_stat = {
'name': f"Копия_{id_stat.name}",
'company': id_stat.company_id,
'analysis': id_stat.analysis_interval,
'active': False,
'debug': id_stat.debug,
'items': []
}
last_stata = Stat.objects.latest('id').id
new_stat['id'] = last_stat + 1
new_stat.save()
Ошибка:
AttributeError: 'dict' object has no attribute 'save'
Прошу помочь найти, в чем проблема.
Ответы (1 шт):
Автор решения: FroggerProgger
→ Ссылка
У вас new_stat - обычный словарь. А не объект Stat.
id_stat = await request.app.m.stat.get_one(stat_id, connection=connection)
if id_stat:
last_stat = Stat.objects.latest('id')
new_stat = Stat(
name=f"Копия_{id_stat.name}",
company=id_stat.company,
analysis=id_stat.analysis,
active=False,
debug=id_stat.debug,
items=[]
)
new_stat.id = last_stat.id + 1
new_stat.save()
Думаю, что так будет работать.