2016-11-23 78 views
1

我創建了一個pre_save信號來檢查一個字段是否已更新,然後才能節省一個模型,如果是這樣的話就拍攝一個webhook。當我從./manage.py shell編輯模型並保存它時,這工作正常,但如果我在使用表單的網站上編輯它,它不會觸發webhook。爲什麼是這樣?Django pre_save信號在保存模型時工作,但不是ModelForm?

apps.py

from django.apps import AppConfig 


class ClientConfig(AppConfig): 
    name = 'client' 
    verbose_name = "Clients" 
    label = 'client' 

    def ready(self): 
     import client.signals 

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') 

回答

1

我不確定它爲什麼不起作用,但我建議您將該功能移至模型保存方法。

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) 

    def save(self, *args, **kwargs): 
     #check if obj is new or being updated 
     try: 
      obj = ClientContact.objects.get(pk=self.pk) 
      if not obj.pipeline == self.pipeline: # Field has changed 
       self.pipeline_updated() 
     except ObjectDoesNotExist: 
      pass 
     #call super and store data 
     super(ClientContact, self).save(*args, **kwargs) 
+0

不知道最初的問題是什麼,但是這讓我不知所措。謝謝! – joshcrim

+0

不客氣! :) – mk2