我只想在用户登录时运行此功能。 我尝试过: 但是它返回: 我该如何替换自我? 答案 0 :(得分:0) 您没有说自己所指的是哪种阶级。但是在这种情况下,我将在该类之外编写代码,该代码在user_log_in上触发,并在发生这种情况时实例化一个用户类。实例化课程后,您可以调用 和以前一样。
如果您想将可以随时运行的功能与仅在用户登录时必须运行的功能分开,那么我将创建2个单独的类,并在user_log_in触发时触发第二个类的实例化。 无论哪种方式,回调的控制代码都应该在类之外。def purchased_today(self):
check = Rating.objects.filter(user=self.user, book__collection=self.collection, score__lte=4).order_by('newest')[:50]
a = check.count()
today = datetime.now().strftime("%Y-%m-%d")
if self.last_checked.date().strftime("%Y-%m-%d") != today:
return a
@receiver(user_logged_in)
def purchased_today(sender, user, request, **kwargs):
check = Rating.objects.filter(user=self.user, book__collection=self.collection, score__lte=4).order_by('newest')[:50]
a = check.count()
today = datetime.now().strftime("%Y-%m-%d")
if self.last_checked.date().strftime("%Y-%m-%d") != today:
return a
name 'self' is not defined.
1 个答案:
purchased_today(self)