将 Python 应用程序转换为 Django

发布于 2024-10-26 05:37:20 字数 282 浏览 1 评论 0原文

我有一个包含大约十几个类的Python 程序,其中几个类拥有其他类的实例,例如ObjectA 有一个ObjectB 的列表,以及一个(ObjectC,ObjectD)对的字典。

我的目标是将程序的功能放在网站上。

我已经为每个类编写并测试了 JSON 编码和解码方法。我现在看到的问题是,我需要选择是从数据库角度重新开始并重新编写模型和逻辑,还是简单地将 python 对象(编码为 JSON)存储在数据库中,并提取保存的状态变化。

有人可以确认这些都是有效的方法,并且我没有错过任何其他简单的选择吗?

I've got a Python program with about a dozen classes, with several classes possessing instances of other classes, e.g. ObjectA has a list of ObjectB's, and a dictionary of (ObjectC, ObjectD) pairs.

My goal is to put the program's functionality on a website.

I've written and tested JSON encode and decode methods for each class. The problem as I see it now is that I need to choose between starting over and writing the models and logic afresh from a database perspective, or simply storing the python objects (encoded as JSON) in the database, and pulling out the saved states for changes.

Can someone confirm that these are both valid approaches, and that I'm not missing any other simple options?

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

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

发布评论

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

评论(1

时光无声 2024-11-02 05:37:20

伙计,我认为你可以做的就是将你已经制作的类转换为 django 模型类。当然,只有那些需要保存到数据库的。其他类和其余代码一样,我建议您封装它们以用作辅助函数。因此,您不必对代码进行太多更改,它就会正常工作。 ;D

或者,另一种更容易实现的选择是:将所有内容放入助手中,包括类、函数和其他所有内容。
因此,您只需要调用视图中的函数并定义模型即可将数据保存到数据库中。

您将对象以 JSON 形式保存在数据库中的想法可行,但很丑陋。 ;)
不管怎样,如果你急于交付网站,任何事情都是有效的。请记住,以这种方式制作的东西总会给我们将来带来很多问题。

希望它有用! :D

Man, what I think you can do is convert the classes you already have made into django model classes. Of course, only the ones that need to be saved to a database. The other classes, as the rest of the code, I recommend you to encapsulate them for use as helper functions. So you don't have to change too much your code and it's going to work fine. ;D

Or, another choice, that can be easier to implement is: put everything in a helper, the classes, the functions and everything else.
SO you'll just need to call the functions in your views and define models to save your data into the database.

Your idea of saving the objects as JSON on the database works, but it's ugly. ;)
Anyway, if you are in a hurry to deliver the website, anything is valid. Just remember that things made in this way always give us lots of problems in the future.

It hopes that it could be useful! :D

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