简体   繁体   English

如何在带有MongoEngine后端的Django中创建简单的REST API?

[英]How do I create simple REST APIs in Django with a MongoEngine backend?

Tastypie looked promising, now not so much: Tastypie看起来很有前途,但现在还不那么多:

http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html

Should I use SimpleAPI or is there a better solution? 我应该使用SimpleAPI还是有更好的解决方案?

The finally solution was to use django-tastypie + django-tastypie-mongoengine: 最终的解决方案是使用django-tastypie + django-tastypie-mongoengine:

https://github.com/mitar/django-tastypie-mongoengine https://github.com/mitar/django-tastypie-mongoengine

Once you have that installed, in your app add a resource.py with code like this: 安装完成后,在您的应用程序中添加带有以下代码的resource.py:

from models.account import MAccount
from models.company import MCompany


class AccountResource(resources.MongoEngineResource):
        class Meta:
                serializer = CustomSerializer()
                queryset = MAccount.objects.all()
                allowed_methods = ('get', 'post', 'put','delete')
                resource_name = 'account'
                authorization= tastypie_authorization.Authorization()

Then if your urls.py file add this code: 然后,如果您的urls.py文件添加以下代码:

v1_api = api.Api(api_name='v1')
v1_api.register(resources.AccountResource())
urlpatterns += patterns('', (r'^m/api/', include(v1_api.urls)))

Finally, you should be able to hit an API like 最后,您应该能够找到一个类似

/m/api/v1/account/?format=json /米/ API / V1 /帐户/?格式= JSON

I once used django-piston . 我曾经用过django-活塞 You should try it out, it's easy to create a rest api and it's integrated with django. 您应该尝试一下,创建rest api很容易,并且它与django集成在一起。

I've read it can be done with MongoEngine, but never tryed. 我读过它可以用MongoEngine完成,但从未尝试过。

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

相关问题 如何在Django Rest Framework中正确使用mongoengine嵌入式文档 - How do I properly use mongoengine embedded document in Django Rest Framework 如何在Django注册中配置简单后端 - How do I configure Simple Backend in Django Registration 如何为django注册创建自定义django后端? - How do i create a custom django backend for django-registration? 如何在mongoengine中创建Django的表单? - How can I create Django's form at mongoengine? 如何使用 django rest 过滤与 mongoengine - how to use django rest filtering with mongoengine 如何在Django REST Framework Mongoengine中序列化EmbeddedDocumentListField? - How to serialize EmbeddedDocumentListField in Django REST Framework Mongoengine? 如何在我的 Django 工作中在 mongoengine 中搜索 null 或 none? - how do I search null or none in mongoengine in my django work? 如何使用Mongoengine(适用于Django的Mongodb模块)在Django Rest Framework中存储和提供图像 - How Can I Store and serve an image in Django Rest Framework using Mongoengine (Mongodb module for Django) 如何在嵌套序列化程序中使用 mongoengine 为 Django-Rest-Framework 定义正确的.create() 方法 - How to define a correctly .create() method for Django-Rest-Framework with mongoengine in nested serializers 如何为 django rest API 生成静态 api 文档? - How can i generate static api documentation for django rest APIs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM