0
私は別のオブジェクトを作成するオブジェクトのperform_createを持っています。objetが管理サイトで作成されたときに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()
私はDocumentDetail
も作成されDocumentDetailSample
を作成するときに、見ることができるように。
「POST」を実行するとうまく動作します。
私の問題は、DocumentDetailSamples
も管理サイトで作成できることです。だから... Adminサイトで作成されたときは、perform_create
はもう動かず、DocumentDetail
は作成されません。
私は何ができますか? "admin" DocumentDetailSample
の作成直後にもDocumentDetail
を作成したいと思います。
おかげで作成されたときにのみ機能を実行するよう
post_save
信号が作成された引数を提供しています!どこでその機能を書くべきですか? views.py?また、 "post_save"をインポートし、受信機で "pre_save"を使用します。どちらが正しいですか? :) –いいね!ありがとう!!! :) だから、ええ、私はviews.pyに書きました、そしてそれはpost_saveです。 _pre_save_に 'created'がありません:)ありがとう! –