是否必须导入同一文件两次?

发布于 2024-12-12 07:28:04 字数 1347 浏览 1 评论 0原文

我有一个模型文件。

从我的视图文件中导入模型文件

从我的视图文件中,我通过添加属性来更改特定函数中的模型文件。

如果我仍然在相同的功能中然后希望访问新的模型属性,那么我是否必须重新导入模型文件

这是我试图完成的任务

在我的模型文件中我有一堆属性来衡量和排名人们的能力举重。他们可以自行衡量和排名 10 种不同的练习。我希望他们混合搭配练习和排名。通过 10 个练习,这就产生了数百种排名可能性,我不想手动编码。这仅适用于重量部分。

如果有人以数百种可能的方式中的任何一种来查找自己的排名,我希望代码检查排名属性是否存在。如果没有,我想将它添加到 mysql 数据库以及模型文件中。

for i in request.GET:
        a += i
        a += '+'
        b += i
        b += '_'

a = a.strip("+")
b = b.strip("_")
c = b + '_rank'

if c not in WeightsProfile.__doc__:


        shutil.move("models.py", "models.py"+"~")
        original = open("models.py"+"~", 'r')
        new = open("models.py", 'w')

        for line in original:
            new.write(line)
            if "Snatch_rank = models.IntegerField(max_length=4, default=0)" in line:
                new.write("    %s = models.IntegerField(max_length=30, default='0')" % c + '\n')

        new.close()
        original.close() 

        conn = mdb.connect('localhost', 'jamie', 'wiser9999', 'website')
        cursor = conn.cursor()
        cursor.execute("alter table mysite_weightsprofile add column %s integer not null; SET @rank=0; UPDATE mysite_weightsprofile SET %s = @rank:=@rank+1 order by %s DESC;" %  (c, c, a))
        cursor.close()
        conn.close()
    else:
        pass 

这是不好的做法吗

I have a models file.

From my views file I import the models file

From my views file I alter the models file in a particular function by adding an attribute.

If while still in the same function i then wish to access the new models attribute, would I then have to re-import the models file

Heres what im trying to accomplish

In my models file i have a bunch of attributes that measures and ranks peoples ability to lift weights. There are 10 different exercises that they can measure and rank themselves. I want them to mix and match the exercises and rankings. With 10 exercises this makes hundreds of ranking possibilities, something i dont wish to code in manually. And this is just for the weights section.

If somebody goes about finding out thier rank in any of the hundreds of possible ways, i want the code to check if the ranking attribute is there. If not, i want to add it to the mysql database as well as the models file.

for i in request.GET:
        a += i
        a += '+'
        b += i
        b += '_'

a = a.strip("+")
b = b.strip("_")
c = b + '_rank'

if c not in WeightsProfile.__doc__:


        shutil.move("models.py", "models.py"+"~")
        original = open("models.py"+"~", 'r')
        new = open("models.py", 'w')

        for line in original:
            new.write(line)
            if "Snatch_rank = models.IntegerField(max_length=4, default=0)" in line:
                new.write("    %s = models.IntegerField(max_length=30, default='0')" % c + '\n')

        new.close()
        original.close() 

        conn = mdb.connect('localhost', 'jamie', 'wiser9999', 'website')
        cursor = conn.cursor()
        cursor.execute("alter table mysite_weightsprofile add column %s integer not null; SET @rank=0; UPDATE mysite_weightsprofile SET %s = @rank:=@rank+1 order by %s DESC;" %  (c, c, a))
        cursor.close()
        conn.close()
    else:
        pass 

Is this bad practice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-12-19 07:28:05

您不是在更改“模型文件”,而是在更改模型在内存中的表示形式——本质上是对您自己的代码进行猴子修补。所以,不,你不必重新导入任何东西——事实上,如果可以的话,你只需覆盖你的更改——但是,这是糟糕的设计。也许如果你更多地解释你想要实现的目标,有人可以告诉你更好的方法来实现它。

You're not altering the "models file", your altering the in-memory representation of the model -- essentially monkey-patching your own code. So, no, you don't have to re-import anything -- in fact, if you could, you'd simply overwrite your changes -- but, this is bad, bad design. Perhaps if you explain more about what you're trying to accomplish, someone can tell you a better way to go about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文