【问题标题】:Django notification observe model (watching for product results)Django 通知观察模型(观察产品结果)
【发布时间】:2011-08-02 11:41:30
【问题描述】:

我一直在使用 django-notification (https://github.com/jtauber/django-notification.git),但文档对于初学者来说有点简短。

我希望能够让用户关注在搜索时没有结果的搜索(包含产品列表的结果页面)。然后,如果添加了与搜索匹配的记录,则应通知用户。

我找不到任何关于如何使用“观察”的在线解释,我认为我需要用它来观察出现的记录(在搜索结果中)?也许,这是错误的方法(使用 django-notification),因为我需要一个信号来等待最初不包含任何对象的过滤器结果的出现......

(该项目太发达了,无法考虑像 Pinax 这样的选项来为此类事情提供模板)


我想我需要评估

f=Products.objects.filter({search_request_args})
if f:
   notification.send([request.user], "product_match", {"from_user": settings.FROM_DEFAULT})

也许作为一个计时工作?

【问题讨论】:

    标签: django signals django-signals django-notification


    【解决方案1】:

    您似乎想使用 django 信号(请参阅:https://docs.djangoproject.com/en/dev/topics/signals/

    假设您想观看Product 对象的创建

    from django.db.models.signals import post_save
    from my_app.models import Product
    
    def new_product(sender, instance, created, **kwargs):
        # short-circuit the function if it isn't a new product (it's 
        # being updated not created)
        if not created: return
    
        # note: instance is the newly saved Product object
    
        if (check_if_the_new_product_matches_searches_here):
            notification.send(...)
    
    post_save.connect(new_product, sender=Product)
    

    【讨论】:

    • 感谢您提供的信息 - 只是担心每次有人添加 new_product 时运行 (check_if_the_new_product_matches_searches_here) 将是一个昂贵的查询。我应该解释一下 new_product 功能对所有用户都可用。非常感谢。
    • @adam 您可以使用 celery 来延迟该处理,以便响应并返回运行昂贵的查询之前。见:celeryproject.org
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    相关资源
    最近更新 更多