没有使用Django信号获取更新的字段

时间:2019-01-28 06:41:14

标签: django django-signals

我正在尝试使用django信号获取更新的字段。当我使用更新视图更新模型并调用post_save时,我在kwargs中将update_fields设置为None。 如何使用Django信号获取更新的字段?

signals.py

from .models import Department
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Department)
def department_history(sender, created, **kwargs):
 update = kwargs['update_fields']

views.py

class DepartmentEditView(LoginRequiredMixin, SuccessMessageMixin,PermissionRequiredMixin, UpdateView):

model = Department
form_class = DepartmentForm
success_url = reverse_lazy('departments:departments')
success_message ="%(department_name)s was Updated Sucessfully "
template_name_suffix = '_update'
permission_required = ('departments.change_department',)

1 个答案:

答案 0 :(得分:0)

只需更新views.py中的代码

def form_valid():
    obj=form.save(commit=False)
    obj.save(update_fields=['field_name'])

更新的字段名称可以使用form.changed_data

获取