从Django自定义信号接收器获取信息 [英] Getting information from Django custom signal receiver

查看:67
本文介绍了从Django自定义信号接收器获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上是我今天的第二个问题,但我想知道...可以从信号处理程序中检索信息.

This is my second question today actually but what I want to know...Is it possible to retrieve information from a signal handler.

我有一个项目列表,将其称为列表,每个项目都在AppA中.每个项目都有两个特征,这些特征保存在不同的应用程序AppB中.

I have a list of items, call it list and each item is in AppA. Each item has a couple of characteristics which are saved in a different app, AppB.

因此,我认为我可以创建一个字典,对字典进行排序并遍历列表中的项目.在每次迭代中,我希望将信号发送到AppB并检索信息,即具有类似

So, I figured that I could maybe create a dictionary, dict and iterate over the items in list. In each iteration, I was hoping to send a signal to AppB and retrieve the information, i.e. have something like

def blob(request):
    dict = {}
    for item in list:
        signal.send(sender=None, id=item.id)
        dict[item] = (char1, char2)
    ...some html request

我的信号处理程序看起来像这样:

My signal handler looks something like this:

def handler(sender, id, **kwargs):
    model2 = Model2.objects.get(id=id)
    a = model2.char1
    b = model2.char2
    return (a, b)

然后我希望能够仅在网页上生成项目及其特征的列表...问题是,显然信号发送方必须发送信号,并取回我想要的信息....甚至有可能:S?

Then I was hoping to be able to just produce a list of the items and their characteristics in the webpage...THe problem is that obviously the signal sender has to send the signal, and get the information back which I want....is that even possible :S?

当前,我收到一条错误消息,提示未定义全局名称'char1'..并且我已将处理程序和信号导入到blob所在的view.py中..so我的问题难解决吗?/应该用另一种方法解决吗?还是我几乎可以肯定在导入内容时犯了一个愚蠢的错误?

Currently, I get an error saying "global name 'char1' is not defined....and I have imported the handlers and signals into the view.py where blob resides....so is my problem just unsolvable? / Should it clearly be solved in another way? Or have I almost certainly made a stupid error with importing stuff?

推荐答案

这实际上并不那么棘手.以为我也许应该发布它是如何解决的.在我看来,我实际上写过

This wasn't actually so tricky. Thought I should perhaps post how it was solved. In my views, I actually wrote

    response_list=signal.send(sender=None, list=list_of_items)

然后我遍历我的response_list,将项目添加到新列表中,如下所示:

I then iterated over my response_list, adding the items to a fresh list like so:

    snippets = []
    for response in response_list:
    logger.error(response)
    snippets.append(response[1])

然后可以像我的模板中的字典一样调用摘要中的响应.当我问这个问题时,我不感激我可以将某些东西等同于信号发送...

And could then call the responses in snippets like a dictionary in my template. When I asked the question, I didn't appreciate that I could equate something with the signal sending...

这篇关于从Django自定义信号接收器获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆