如何为我的应用程序实现内置信号?

时间:2018-10-21 16:49:41

标签: django

我有一个项目myappointments,其中有两个应用程序-约会和诊所。

目标: 用户登录时,应在数据库中输入详细信息。

appointments / models.py:

class Event(models.Model):
    id=models.AutoField(primary_key=True, unique=True)
    type=models.CharField(max_length=60)
    description = models.CharField(max_length=150)
    time = models.DateTimeField(default=timezone.now)

约会/ 初始化 .py:

default_app_config = 'appointments.apps.AppointmentsConfig'

appointments / apps.py:

from django.apps import AppConfig
class AppointmentsConfig(AppConfig):
    name = 'appointments'
    def ready(self):
        import appointments.signals 

约会/signals.py:

from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
def record_loggedin(sender, user, request, **kwargs):
  ev = Event(type="Login", description = 'User logged in: {}'.format(request.user.id))
  ev.save()
  print("User has logged in. Saved event to DB.")
user_logged_in.connect(record_loggedin)

我还需要进行哪些其他修改?

0 个答案:

没有答案