【问题标题】:Combine 2 response if models.field is same for two requests如果两个请求的 models.field 相同,则合并 2 个响应
【发布时间】:2017-02-06 16:55:55
【问题描述】:

我有 idtypelanguage 的请求数据模型。我正在做以下过程。

def email_me(requests):`
    string = ""
    for request in requests.all():
        string = """
        ID: %d
        Type: %s
        """ % (request.id, request.type)
    <some code for sending an email>

我正在将这个函数运行到一个视图中,在那里我得到了requests

这里string 将为每个单独的请求数据库列提供数据。我想要做的是,如果type 对于两个字符串相同,我想将它们组合在一封电子邮件中。例如type = customer 我想在一封电子邮件中发送数据。

我该怎么做?

【问题讨论】:

    标签: django python-2.7 if-statement for-loop django-models


    【解决方案1】:

    你可以尝试使用python字典:

    to_send = dict()
    for request in requests.filter(type=type):
        string = """
        ID: %d
        Type: %s
        """ % (request.id, request.type)
        if request.type not in to_send:
            to_sent[type] = ''
        to_send[type] += string
    
    for type, string in to_send.iteritems():
        # send you mail
        pass
    

    请注意,如果该表中的数据过多,可能会有更优化的解决方案,我给了你一个简单/快速的解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 2020-11-30
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多