如题所述
ãå 为æ°çdjangoå¼å人åå¾ç¥signalä¹å, å¾å¾ä¼å¾é«å
´å»ä½¿ç¨å®. ä»ä»¬å¨è½ä½¿ç¨signalçå°æ¹å°±ä½¿ç¨signal, 并ä¸è¿æ¯ä»ä»¬è§å¾èªå·±æ¯djangoä¸å®¶ä¸æ ·. ç¶è, åè¿æ ·ç¼ç ä¸æ®µæ¶é´å, django项ç®å°±ä¼åå¾å¼å¸¸å¤æ, 许å¤å
容é½çº ç»å¨ä¸èµ·æ æ³è§£å¼.
ããç®ååçæ¯: å¨å ¶ä»æ¹æ³æ æ³ä½¿ç¨çæ åµä¸, ææåèè使ç¨signal.
ããå 为æ°çdjangoå¼å人åå¾ç¥signalä¹å, å¾å¾ä¼å¾é«å ´å»ä½¿ç¨å®. ä»ä»¬å¨è½ä½¿ç¨signalçå°æ¹å°±ä½¿ç¨signal, 并ä¸è¿æ¯ä»ä»¬è§å¾èªå·±æ¯djangoä¸å®¶ä¸æ ·. ç¶è, åè¿æ ·ç¼ç ä¸æ®µæ¶é´å, django项ç®å°±ä¼åå¾å¼å¸¸å¤æ, 许å¤å 容é½çº ç»å¨ä¸èµ·æ æ³è§£å¼.
ãã许å¤å¼åè ä¹ä¼å°django signalåå¼æ¥æ¶æ¯åé(ä¾å¦celery)ææ··. signalæ¯åæ¥å¤ç, å æ¤éè¿signalè°ç¨å¤§å¤çéçè¿ç¨æ¶å¹¶æ æ³æé«æ§è½. äºå®ä¸, å°è¿äºéè¦å¤§å¤çéçè¿ç¨ç§»å°signalä¸è¢«è§ä½æ¯ä¸ç§ä¸å¥½çä¹ æ¯.
ãã1. ä½æ¶ä½¿ç¨signal
ãã以ä¸æ åµä¸è¦ä½¿ç¨signal:
ããsignalä¸ä¸ä¸ªmodelç´§å¯ç¸å ³, 并è½ç§»å°è¯¥modelçsave()æ¶
signalè½ä½¿ç¨model manager代æ¿æ¶
signalä¸ä¸ä¸ªviewç´§å¯ç¸å ³, 并è½ç§»å°è¯¥viewä¸æ¶
以ä¸æ åµå¯ä»¥ä½¿ç¨signal:
ããsignalçreceiveréè¦åæ¶ä¿®æ¹å¯¹å¤ä¸ªmodelæ¶
å°å¤ä¸ªappçç¸åsignalå¼å°åä¸receiverä¸å¤çæ¶
å¨æä¸modelä¿åä¹åå°cacheæ¸ é¤æ¶
æ æ³ä½¿ç¨å ¶ä»æ¹æ³, ä½éè¦ä¸ä¸ªè¢«è°å½æ°æ¥å¤çæäºé®é¢æ¶
2. Signalç代æ¿æ¹æ³
ãã使ç¨modèæ¥manager
ãã以ä¸ä»£ç æ¼ç¤ºäºå½ç¨æ·å建Event modelæ¶, éè¦éç¥ç®¡çå, å¦ææ¹åmodelä¸çpost_save(), åéè¦æ·»å é¢å¤çé»è¾æ¥åºåç¨æ·è¿æ¯ç®¡çå:
ãã# myapp/managers.py
ããfrom django.db import models
ãã
ããclass EventManager(models.Manager):
ãã
ããdef create_event(self, title, start, end, creator):
ããevent = self.model(title=title, start=start, end=end, creator=creator)
ããevent.save()
ããevent.notify_admins()
ããreturn event
ãã
ããå¨modelä¸è®¾ç½®model manager:
ã
ãã# myapp/models.py
ããfrom django.conf import settings
ããfrom django.core.mail import mail_admins
ããfrom django.db import models
ãã
ããfrom model_utils.models import TimeStampedModel
ããfrom .managers import EventManager
ãã
ããclass Event(TimeStampedModel):
ãã
ããSTATUS_UNREVIEWED, STATUS_REVIEWED = (0, 1)
ããSTATUS_CHOICES = (
ãã(STATUS_UNREVIEWED, "Unreviewed"),
ãã(STATUS_REVIEWED, "Reviewed")
ãã)
ãã
ããtitle = models.CharField(max_length=100)
ããstart = models.DateTimeField()
ããend = model.dateTimeField()
ããstatus = models.IntegerField(choices=STATUS_CHOICES, default=STATUS_UNREVIEWED)
ããcreator = models.ForeignField(settings.AUTH_USER_MODEL)
ããobjects = EventManager()
ãã
ããdef notify_admins(self):
ããsubject = "{user} submitted a new event!".format(user=self.creator.get_full_name())
ããmessage = """TITLE: {title}
ããSTART: {start}
ããEND: {end}""".format(title=self.title, start=self.start, end=self.end)
ããmail_admins(subject=subject, message=message, fail_silently=False)
ãã
ããå¨viewä¸ä½¿ç¨create_event()代æ¿create()æ¶, 便ä¼éç¥ç®¡çåäº.
ããå¨å ¶ä»ä»£ç ä¸éªè¯model
ããå¦æä½ ä½¿ç¨pre_save signalæ¥éªè¯æä¸model, ååºå½å°è¯èªå·±åä¸ä¸ªvalidatorå代ä¹. å¦æéªè¯æ¯éè¿ModelFormæ¶, éè¿æ¹åclean()å®ç°éªè¯.
ãã使ç¨modelçsave()ådelete()
ããå¦æ使ç¨pre_save æ post_save signal, å¦æå¯ä»¥, åå°è¿äºä»£ç 移å°modelçsave()æ¹æ³ä¸.
ããåæ ·å¦æ使ç¨pre_delete æ post_delete signal, å¦æå¯ä»¥, åå°è¿äºä»£ç 移å°modelçdelte()æ¹æ³ä¸.
ãã使ç¨å ¶ä»ä»£ç 代æ¿signal
ããå¦æå¯è½, æ们å¯ä»¥å°signalçé»è¾ä½¿ç¨å ¶ä»å¸®å©ç¨åºå®ç°.
ããç®ååçæ¯: å¨å ¶ä»æ¹æ³æ æ³ä½¿ç¨çæ åµä¸, ææåèè使ç¨signal.
ããå 为æ°çdjangoå¼å人åå¾ç¥signalä¹å, å¾å¾ä¼å¾é«å ´å»ä½¿ç¨å®. ä»ä»¬å¨è½ä½¿ç¨signalçå°æ¹å°±ä½¿ç¨signal, 并ä¸è¿æ¯ä»ä»¬è§å¾èªå·±æ¯djangoä¸å®¶ä¸æ ·. ç¶è, åè¿æ ·ç¼ç ä¸æ®µæ¶é´å, django项ç®å°±ä¼åå¾å¼å¸¸å¤æ, 许å¤å 容é½çº ç»å¨ä¸èµ·æ æ³è§£å¼.
ãã许å¤å¼åè ä¹ä¼å°django signalåå¼æ¥æ¶æ¯åé(ä¾å¦celery)ææ··. signalæ¯åæ¥å¤ç, å æ¤éè¿signalè°ç¨å¤§å¤çéçè¿ç¨æ¶å¹¶æ æ³æé«æ§è½. äºå®ä¸, å°è¿äºéè¦å¤§å¤çéçè¿ç¨ç§»å°signalä¸è¢«è§ä½æ¯ä¸ç§ä¸å¥½çä¹ æ¯.
ãã1. ä½æ¶ä½¿ç¨signal
ãã以ä¸æ åµä¸è¦ä½¿ç¨signal:
ããsignalä¸ä¸ä¸ªmodelç´§å¯ç¸å ³, 并è½ç§»å°è¯¥modelçsave()æ¶
signalè½ä½¿ç¨model manager代æ¿æ¶
signalä¸ä¸ä¸ªviewç´§å¯ç¸å ³, 并è½ç§»å°è¯¥viewä¸æ¶
以ä¸æ åµå¯ä»¥ä½¿ç¨signal:
ããsignalçreceiveréè¦åæ¶ä¿®æ¹å¯¹å¤ä¸ªmodelæ¶
å°å¤ä¸ªappçç¸åsignalå¼å°åä¸receiverä¸å¤çæ¶
å¨æä¸modelä¿åä¹åå°cacheæ¸ é¤æ¶
æ æ³ä½¿ç¨å ¶ä»æ¹æ³, ä½éè¦ä¸ä¸ªè¢«è°å½æ°æ¥å¤çæäºé®é¢æ¶
2. Signalç代æ¿æ¹æ³
ãã使ç¨modèæ¥manager
ãã以ä¸ä»£ç æ¼ç¤ºäºå½ç¨æ·å建Event modelæ¶, éè¦éç¥ç®¡çå, å¦ææ¹åmodelä¸çpost_save(), åéè¦æ·»å é¢å¤çé»è¾æ¥åºåç¨æ·è¿æ¯ç®¡çå:
ãã# myapp/managers.py
ããfrom django.db import models
ãã
ããclass EventManager(models.Manager):
ãã
ããdef create_event(self, title, start, end, creator):
ããevent = self.model(title=title, start=start, end=end, creator=creator)
ããevent.save()
ããevent.notify_admins()
ããreturn event
ãã
ããå¨modelä¸è®¾ç½®model manager:
ã
ãã# myapp/models.py
ããfrom django.conf import settings
ããfrom django.core.mail import mail_admins
ããfrom django.db import models
ãã
ããfrom model_utils.models import TimeStampedModel
ããfrom .managers import EventManager
ãã
ããclass Event(TimeStampedModel):
ãã
ããSTATUS_UNREVIEWED, STATUS_REVIEWED = (0, 1)
ããSTATUS_CHOICES = (
ãã(STATUS_UNREVIEWED, "Unreviewed"),
ãã(STATUS_REVIEWED, "Reviewed")
ãã)
ãã
ããtitle = models.CharField(max_length=100)
ããstart = models.DateTimeField()
ããend = model.dateTimeField()
ããstatus = models.IntegerField(choices=STATUS_CHOICES, default=STATUS_UNREVIEWED)
ããcreator = models.ForeignField(settings.AUTH_USER_MODEL)
ããobjects = EventManager()
ãã
ããdef notify_admins(self):
ããsubject = "{user} submitted a new event!".format(user=self.creator.get_full_name())
ããmessage = """TITLE: {title}
ããSTART: {start}
ããEND: {end}""".format(title=self.title, start=self.start, end=self.end)
ããmail_admins(subject=subject, message=message, fail_silently=False)
ãã
ããå¨viewä¸ä½¿ç¨create_event()代æ¿create()æ¶, 便ä¼éç¥ç®¡çåäº.
ããå¨å ¶ä»ä»£ç ä¸éªè¯model
ããå¦æä½ ä½¿ç¨pre_save signalæ¥éªè¯æä¸model, ååºå½å°è¯èªå·±åä¸ä¸ªvalidatorå代ä¹. å¦æéªè¯æ¯éè¿ModelFormæ¶, éè¿æ¹åclean()å®ç°éªè¯.
ãã使ç¨modelçsave()ådelete()
ããå¦æ使ç¨pre_save æ post_save signal, å¦æå¯ä»¥, åå°è¿äºä»£ç 移å°modelçsave()æ¹æ³ä¸.
ããåæ ·å¦æ使ç¨pre_delete æ post_delete signal, å¦æå¯ä»¥, åå°è¿äºä»£ç 移å°modelçdelte()æ¹æ³ä¸.
ãã使ç¨å ¶ä»ä»£ç 代æ¿signal
ããå¦æå¯è½, æ们å¯ä»¥å°signalçé»è¾ä½¿ç¨å ¶ä»å¸®å©ç¨åºå®ç°.
温馨提示:答案为网友推荐,仅供参考