0
我節省了使用如何在Django中保存後立即獲取對象?
new_document = form.save(commit=False)
new_document.object_id = int(self.kwargs['pk'])
new_document.content_type = ContentType.objects.get_for_model(self.target_model)
# Set the document category
new_document.category = self.get_document_category()
new_document.save()
什麼是現在想知道的是,我以後保存對象,我怎麼能獲取對象和其他屬性的主鍵的新對象?
保存後對象中是否已存在id?嘗試new_document.id – szaman
或new_document.pk,以防您的主鍵字段有其他名稱 –
此代碼中的「self」是什麼?代碼從哪裏來? – miki725