0
我有一個perform_create對象,它創建另一個對象。如何在管理站點中創建對象時執行perfom_create。 Django RestFramework
class DocumentDetailSampleViewSet(viewsets.ModelViewSet):
queryset = DocumentDetailSample.objects.all()
serializer_class = DocumentDetailSampleSerializer
def perform_create(self, serializer):
doc_detail_sample = serializer.save()
query_set = User.objects.all()
ids = query_set.values_list('id', flat=True)
for i in ids:
doc_detail = DocumentDetail()
doc_detail.details_sample = doc_detail_sample
doc_detail.user_id_id = str(i)
doc_detail.save()
正如你可以看到,當我創建一個DocumentDetailSample
也創造了一個DocumentDetail
。
當我做一個「POST」時,它很好地工作。
我的問題是,DocumentDetailSamples
也可以在管理網站中創建。所以......當它在Admin站點中創建時,perform_create
不再有效,DocumentDetail
未創建。
我該怎麼辦?我想在創建「admin」DocumentDetailSample
之後創建DocumentDetail
。
謝謝!我應該在哪裏編寫該功能?在views.py?此外,您導入「post_save」,然後在接收器中使用「pre_save」。哪一個是正確的? :) –
好!謝謝!!! :) 所以,是的,我把它寫在views.py中,它是post_save。 _pre_save_沒有「創建」:)謝謝! –