简体   繁体   English

如何从另一个py文件调用函数到django中的模型类

[英]How to call a function from another py file to a model class in django

I have created a seprated py file that includes multiple functions that i want to use inside a model class in django,我创建了一个单独的 py 文件,其中包含我想在 django 中的模型类中使用的多个函数,
the file: a.py contains functions eg.文件:a.py 包含函数,例如。

""" code""" “““ 代码”””

def cra_2(lower_threshold, upper_thershold, maximum_cra_payment, market_rent,
          weekly_income, maintenance):

    rent_component = weekly_income * .25
    weekly_maintenance = (maintenance / 365) * 7 * .25
    family_rent = rent_component + weekly_maintenance

    if family_rent > market_rent and market_rent > upper_thershold:
        rent_charged = market_rent
    elif family_rent <= lower_threshold:
        rent_charged = min(market_rent, family_rent)
    elif family_rent <= (lower_threshold + 0.25 *
                         (upper_thershold - lower_threshold)):
        rent_charged = min(market_rent,
                           4 * (family_rent - 0.75 * lower_threshold))
    elif family_rent <= upper_thershold and market_rent > upper_thershold:
        rent_charged = min(market_rent, (family_rent + maximum_cra_payment))
    elif family_rent <= upper_thershold and market_rent < upper_thershold:
        rent_charged = min(market_rent,
                           4 * (family_rent - 0.75 * lower_threshold))
    else:
        rent_charged = min(market_rent, (family_rent + maximum_cra_payment))

    #Calculate CRA

    if rent_charged <= lower_threshold:
        cra_rate = 0.0
    elif rent_charged >= upper_thershold:
        cra_rate = maximum_cra_payment
    else:
        cra_rate = (rent_charged - lower_threshold) * .75

    import json
    report = {
        "rent charged": rent_charged,
        "cra rate: ": cra_rate,
        "family rent": family_rent,
        "given values ":[{"rent_component":rent_component},
        {"weekly_maintenance ":float(weekly_maintenance)


        }
        ]

    }
    return json.dumps(report, indent=2)

i want to use this function in a model class, and the input parameter values are within this class.我想在模型类中使用这个函数,并且输入参数值在这个类中。

Use python import ,使用 python 导入,

import a 

if you want to only import some function then use this如果您只想导入某些功能,请使用此功能

from a import some_func

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM