我很新。如何在django admin中过度保存按钮获取内联对象,就像在models.py文件中的普通模型保存按钮一样?
def save(self, *args, **kwargs):
try:
task_update = Task.objects.get(id=self.task_id)
if self.id is not None:
task_update.is_assigned = False
task_update.save()
except Exception, e:
print e
super(TaskAssignment, self).save(*args, **kwargs)
答案 0 :(得分:0)
在花了很多钱之后,我能够回答我自己的问题。在admin.py和over ride save方法models.py类中添加此代码。
from django.forms.models import BaseInlineFormSet
class BookInline(object):
model = Book
form = BookForm
fromset = BookFormSet
form_layout = (
...
)
class BookInlineFormSet(BaseInlineFormSet):
def save_new_objects(self, commit=True):
saved_instances = super(BookInlineFormSet,self).save_new_objects(commit)
if commit:
# create book for press
return saved_instances
def save_existing_objects(self, commit=True):
saved_instances=super(BookInlineFormSet,self).save_existing_objects(commit)
if commit:
# update book for press
return saved_instances