Django模型对象批量更新

时间:2016-02-01 06:20:02

标签: django

我有一个reset_company_limits模块用于更新公司的限制,这个任务每天早上都在运行。但不知何故,某些公司限制无法更新。我无法在代码中找到任何错误

def reset_company_limits(comp_id=None, reset_type=['daily', 'monthly']):
    if comp_id:
        company_accounts = CompanyAccount.objects.filter(id=comp_id, enabled=1)
    else:
        company_accounts = CompanyAccount.objects.filter(enabled=1)

    reset, rcache, cache_rec = ({} for l in xrange(3))
    error = []
    for rtype in reset_type:
        for account in company_accounts:
            try:
                counts = account.get_feature_reset_counts(feature_type=reset_type)
                limits = account.get_limits()
                recruiters = account.recruiter_set.filter(is_active=True).values('pk')
                for limit in limits:
                    key = RECRUITER_LOOKUP[limit.type]
                    if key in counts[rtype]:
                        if rtype == 'daily':
                            limit.daily = counts[rtype][key]
                        elif rtype == 'monthly':
                            limit.monthly = counts[rtype][key]
                    limit.save()
            except Exception, e:
                error.append(account.pk)
                print "Error occurred for company id %s :: %s" % (account.pk, e)
    return reset, rcache, cache_rec, error

0 个答案:

没有答案