Django - 放置某些代码的位置

时间:2012-12-03 22:21:35

标签: django django-models django-views

我想要一些处理缓存地理编码结果的代码,但我不确定放置它的位置。一个特定的.py文件?自定义经理?一种模型方法?查看?

这段代码基本上会触发请求,操纵/存储模型中的结果并缓存数据。

我应该在哪里放置处理许多内容(请求,模型,缓存)的代码?

1 个答案:

答案 0 :(得分:0)

由于它们都与geo相关,因此我将它放在自己的模块中(即geo.py),然后在其中创建辅助方法和类,您可以在其他模块中导入和使用它们。例如,我只是猜测你需要的功能是什么,可能是geo.py中的类似内容:

class GeoConnection(object):
    def __init__(self, whatever, init, vars, here):
        # initialize connection

    def get_country_code(self, some_var):
        # fire off request and return value

class GeoCache(object):
    def store(self, key, value):
        # code

    def retrieve(self, key):
        # code

然后在其他模块中,您可以执行以下操作:

from your_app.geo import GeoConnection

gc = GeoConnection(whatever, init, vars, here)
some_model.country_code = gc.get_country_code(some_var)
some_model.save()
相关问题