【发布时间】:2017-04-07 17:25:41
【问题描述】:
我创建了一个 pre_save 信号来检查在保存模型之前字段是否已更新,如果是,则关闭 webhook。当我从 ./manage.py shell 编辑模型并保存它时,这工作正常,但如果我在使用表单的网站上编辑它,它不会触发 webhook。这是为什么呢?
apps.py
from django.apps import AppConfig
class ClientConfig(AppConfig):
name = 'client'
verbose_name = "Clients"
label = 'client'
def ready(self):
import client.signals
信号.py
@receiver(pre_save, sender=ClientContact)
def send_hook_on_roadmap_update(sender, instance, **kwargs):
"""Watches for ClientContacts to be saved and checks to see if the
pipeline attribute has changed.
"""
try:
obj = sender.objects.get(pk=instance.pk)
except sender.DoesNotExist:
pass # Object is new, so field hasn't technically changed
else:
if not obj.pipeline == instance.pipeline: # Field has changed
instance.pipeline_updated()
models.py
class ClientContact(models.Model):
title = models.CharField(_("Title"), max_length=50, blank=True, null=True)
first_name = models.CharField(_("First name"), max_length=30, )
last_name = models.CharField(_("Last name"), max_length=30, )
email = models.EmailField(_("Email"), blank=True, max_length=75)
create_date = models.DateField(_("Creation date"))
address = models.ForeignKey('AddressBook', blank=True, null=True, on_delete=models.SET_NULL)
pipeline = models.ForeignKey(Pipeline, blank=True, null=True, on_delete=models.SET_NULL)
forms.py
class ContactModelForm(forms.ModelForm):
def __init__(self, client, organization, *args, **kwargs):
super(ContactModelForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
'first_name',
'last_name',
'title',
'email',
'address',
'pipeline',
)
self.helper.form_class = 'form-horizontal'
self.helper.form_method = 'post'
self.helper.html5_required = True
self.helper.add_input(Submit('contact_save', 'Save'))
self.fields['address'].queryset = AddressBook.objects.filter(organization=client)
self.fields['pipeline'].queryset = Pipeline.objects.filter(organization=organization)
class Meta:
model = ClientContact
fields = ('first_name', 'last_name', 'title', 'email', 'address', 'pipeline')
【问题讨论】:
标签: python django python-2.7 django-models django-forms