管理员端修改django
Admin side modification django
我有这个 class 当我提交一个名字时它会转到管理员并且只有管理员可以批准这个。我希望当管理员批准时自动向用户发送一封电子邮件。
class myab(models.Model):
generic_name = models.CharField(max_length=50, null=False)
timestamp = models.DateTimeField(auto_now_add=True)
is_approved = models.BooleanField(null=False, default=False)
我只想知道如何触发电子邮件代码。我还有其他一切只是想了解当管理员批准此功能时如何触发该功能post。
您可以为 post-save signal and use a if
to check if the instance was approved; read the signal docs 创建一个侦听器函数以便更好地理解。
信号接收器可能与此类似:
from django.core import mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, *args, **kwargs):
...
if instance.is_approved:
mail.send_mail(...)
Where should this code live?
[...] signal handling [...] code can live
anywhere you like, although it’s recommended to avoid the
application’s root module and its models module [...]
In practice, signal handlers are usually defined in a signals
submodule of the application they relate to [...]
我有这个 class 当我提交一个名字时它会转到管理员并且只有管理员可以批准这个。我希望当管理员批准时自动向用户发送一封电子邮件。
class myab(models.Model):
generic_name = models.CharField(max_length=50, null=False)
timestamp = models.DateTimeField(auto_now_add=True)
is_approved = models.BooleanField(null=False, default=False)
我只想知道如何触发电子邮件代码。我还有其他一切只是想了解当管理员批准此功能时如何触发该功能post。
您可以为 post-save signal and use a if
to check if the instance was approved; read the signal docs 创建一个侦听器函数以便更好地理解。
信号接收器可能与此类似:
from django.core import mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, *args, **kwargs):
...
if instance.is_approved:
mail.send_mail(...)
Where should this code live?
[...] signal handling [...] code can live anywhere you like, although it’s recommended to avoid the application’s root module and its models module [...]
In practice, signal handlers are usually defined in a signals submodule of the application they relate to [...]