使用Django信号获取absolute_url

时间:2019-02-25 15:43:39

标签: python django

我想从保存的对象中获取absolute url。我的模型有一个名为get_absolute_url的方法,我想用我的Django post_save signal调用此方法。

当在名为post_save signal的特定表中添加新条目时,我收到Thread。这个post_save信号执行我的芹菜任务。

我的线程模型是:

class Thread(models.Model):
    """ A thread with a title """

    topic = models.ForeignKey('Topic')
    title = models.CharField(max_length=200)
    sticky = models.BooleanField(default=False)
    slug = models.SlugField()
    time_created = models.DateTimeField(default=timezone.now)
    time_last_activity = models.DateTimeField(default=timezone.now)

    def __init__(self, *args, **kwargs):
        """ Initialize 'time_last_activity' to 'time_created' """
        super(Thread, self).__init__(*args, **kwargs)

        self.time_last_activity = self.time_created

    def __str__(self):
        """ Return the thread's title """
        return self.title

    def get_absolute_url(self):
        """ Return the url of the instance's detail view """
        url_kwargs = {
            'topic_pk': self.topic.pk,
            'topic_slug': self.topic.slug,
            'thread_pk': self.pk,
            'thread_slug': self.slug,
        }
        return reverse('simple-forums:thread-detail', kwargs=url_kwargs)

在我的模型中,我有一个celery.py文件:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db.models.signals import post_save
from django.dispatch import receiver
from simple_forums.models import Thread

from ..tasks import thread_notification


@receiver(post_save, sender=Thread)
def get_new_thread(sender, instance, **kwargs):
    """ Post_save method which start Celery task to notify forum subscribers that a new thread has been created """
    url = Thread.get_absolute_url()
    print(url)
    thread_title = instance.title
    thread_id = instance.id
    topic_id = instance.topic_id
    topic_slug = instance.topic.slug
    topic_title = instance.topic.title
    thread_notification.delay(thread_id=thread_id, thread_title=thread_title, topic_id=topic_id, topic_slug=topic_slug,
                              topic_title=topic_title)

在我的task.py文件中:

# -*- coding: utf-8 -*-

from celery import shared_task
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _

User = get_user_model()


@shared_task(bind=True, time_limit=3600, soft_time_limit=3600)
def thread_notification(self):
    print('Celery task executed')
    return ['success_message', _('Celery task ended')]

我想获取absolute_url,以便使用新的线程路径发送电子邮件。

我的问题是:如果我没有特定的观点(不是必须的),我如何拿起get_absolute_url或使用request.build_absolute_uri

1 个答案:

答案 0 :(得分:2)

这里:

@receiver(post_save, sender=Thread)
def get_new_thread(sender, instance, **kwargs):
    url = Thread.get_absolute_url()

保存的Thread实例是(惊讶)您的instance参数,因此您需要:

    url = instance.get_absolute_url()

在类上调用实例方法没有任何意义(nb:除了几个特定的​​极端情况,然后您必须将实例作为第一个参数传递,但是当您需要它时,我们不再赘述您会知道它是如何工作的。)

现在,由于您使用的是同一个应用程序,请在此处makes no sense eitheris actually an antipattern使用信号。信号的重点是允许应用对其他应用发出的事件做出反应。在这里,您的代码应该非常简单地位于Thread.save()中。