简体   繁体   English

Deliciouspie和django项目结构

[英]Tastypie and django project structure

I am new to Django. 我是Django的新手。 I want to include rest api to my project using tastypie framework. 我想使用restepie框架将rest api包含到我的项目中。 I want to know what are best practices of including tastypie in project. 我想知道在项目中包括好吃的最佳实践是什么。 I see two options: 我看到两个选择:

  1. Separate app where all api related modules are stored (this app will end up rather fat) 存储所有与api相关的模块的单独应用程序(此应用程序最终会很胖)
  2. api folder in every app with api resources for this particular app (not sure that understand how to organize urls in this case) 每个应用程序中的api文件夹,都有该特定应用程序的api资源(不确定是否在这种情况下了解如何组织网址)

So what is the best practice for this? 那么,最佳实践是什么?

Thanks 谢谢

Actually I prefer the first approach. 实际上,我更喜欢第一种方法。 I create an api folder and then i create subfolders for each separate application. 我创建一个api文件夹,然后为每个单独的应用程序创建子文件夹。 Each folder contains an api.py file for the specific need. 每个文件夹都包含一个针对特定需求的api.py文件。

This way it is really simple and clear to support versions in the api and make it more clear. 这样,在api中支持版本并使其更加清晰就非常简单明了。 Also in the api folder i keep a urls.py which contains all the apis from the subfolders and it is easier to maintain. 同样在api文件夹中,我保留了一个urls.py,其中包含子文件夹中的所有api,并且更易于维护。

EXAMPLE

api/ API /


app1/ api.py

app2/ api.py

urls.py

and the urls.py should look a lot like, urls.py应该看起来很像,

from tastypie.api import Api

from .app1.api import App1Resource
from .app2.api import App2Resource


api = Api(api_name='myPath')
api.register(App1Resource())
api.register(App2Resource())


urlpatterns = api.urls

This is of-course just my personal preference which have helped me over the years to maintain a variety of different apis. 当然,这只是我个人的喜好,多年来帮助我保持了各种不同的api。 I hope i helped a bit. 我希望我能有所帮助。

Regards, Michael. 问候,迈克尔。

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

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