使用Django信号获取absolute_url

问题描述 投票:0回答:1

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

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

我的Thread模型是:

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)

在我的tasks.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

python django
1个回答
2
投票

这里:

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

保存的qazxsw poi实例是(惊讶,惊讶)你的qazxsw poi参数,所以你想:

Thread

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

现在,因为你在同一个应用程序中,在这里使用信号instance url = instance.get_absolute_url() 。信号的关键是允许应用程序对其他应用程序发出的事件做出反应。在这里,您的代码应该非常简单地在makes no sense either中。

© www.soinside.com 2019 - 2024. All rights reserved.