Save the related objects before the actual object being edited on django admin

前端 未结 4 1054
感情败类
感情败类 2020-12-08 22:33

Is it possible to save the related objects before the actual object being edited on a django admin form?

For example:

in models.py



        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 22:47

    The following will save the children first:

    class ParentAdmin(admin.ModelAdmin):
        inlines = [ChildInline]
    
        def save_model(self, request, obj, form, change):
            pass # don't actually save the parent instance
    
        def save_formset(self, request, form, formset, change):
            formset.save() # this will save the children
            form.instance.save() # form.instance is the parent
    

提交回复
热议问题