2012-10-04 55 views
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() 

什麼是現在想知道的是,我以後保存對象,我怎麼能獲取對象和其他屬性的主鍵的新對象?

+0

保存後對象中是否已存在id?嘗試new_document.id – szaman

+0

或new_document.pk,以防您的主鍵字段有其他名稱 –

+0

此代碼中的「self」是什麼?代碼從哪裏來? – miki725

回答

2

您已擁有new_document中的對象。

相關問題