Django:从命令lin生成python代码

2024-04-24 13:26:43 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有没有一种编写Django命令来自动生成代码的方法?你知道吗

在我的例子中:每次我创建一个新模型时,我也必须创建以下内容:

  • admin.py中创建管理类
  • 创建与此模型相关的服务功能。你知道吗
  • 使用FactoryBoy创建工厂。你知道吗
  • 创建测试类。你知道吗

如果有一个自动生成这些内容的命令就好了。当然,不是所有的,只是基本的,定义。你知道吗

今天在Django有这样的事吗?或者有没有一种方法可以编写Django命令来生成代码?你知道吗


Tags: django方法代码py模型命令功能内容
2条回答

试试django命令和jinja2模板。你知道吗

通过执行自定义命令,可以使用适当的内容更新文件模板集,并根据需要将其复制到相应的文件夹中。你知道吗

我还没有亲自使用它,但你可以尝试使用第三方软件包 Django baker Django Baker提供了这种功能

Django Baker wants to help you get your projects up and running quickly. Given one or more app names, s/he will automatically generate views, forms, urls, admin, and templates for all of the models in the models.py file. All files are pep-8 compliant (with exception to the maximum line length rule, which I don't agree with).

Once you add a single urlpattern to your project's URLconf, you'll have a working list view, detail view, create view, update view, and delete view for each model in your app.

Optionally you may specify which models in an app to bake if you'd rather not generate files for all of them.

相关问题 更多 >