我对Django和它的生态系统相对较新.我正在使用django-tastypie为我们的移动客户端编写REST api.我已经浏览了网上几乎所有关于如何使用tastypie创建REST接口的例子.但是它们都不是特定于从客户端发布数据以及如何授权客户端.
我使用了示例中显示的tastypie.authentication.BasicAuthentication.它打开一个弹出窗口询问用户名和密码,并在浏览器上正常工作.但我不确定,它是否会在移动设备上做同样的事情(具体来说,本机IOS应用程序).当用户没有使用浏览器而是使用本机应用程序时,如果用户将请求登录如何在他/她的移动设备上显示此弹出窗口,我就不太了解.
我完全迷失了,我真的很感激你的帮助.
我正在使用django-tastypie为我的项目构建API.我跟着tastypie-doc.
使用此文档,我可以调用GET方法并根据参数过滤数据.但我找不到PUT(UPDATE),DELETE(删除对象)和POST(创建新对象)的任何示例.
有谁知道如何在django-tastypie中编写调用来创建,更新和删除?
谢谢大家.
我们将我们的API从Django - Piston移植到Django-TastyPie.一切顺利,'直到我们达到这个目的:
应用程序的urls.py
url(r'^upload/', Resource(UploadHandler, authentication=KeyAuthentication()), name="api-upload"),
url(r'^result/(?P<uuid>[^//]+)/', Resource(ResultsHandler, authentication=KeyAuthentication()), name="api-result")
Run Code Online (Sandbox Code Playgroud)
这使用活塞,所以我们想把它改成适合TastyPie的东西
url(r'^upload/', include(upload_handler.urls), name="api-upload"),
url(r'^result/(?P<uuid>[^//]+)/', include(results_handler.urls), name="api-result")
Run Code Online (Sandbox Code Playgroud)
但我们坚持这个错误
使用参数'()'和关键字参数'{'uuid':'fbe7f421-b911-11e0-b721-001f5bf19720'}'找不到'api-result'.
和结果的调试页面:
使用MelodyService.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式:
^ melotranscript/^ upload/^ melotranscript/^ result /(?P [^ //] +)/ ^(?Presultshandler)/ $ [name ='api_dispatch_list'] ^ melotranscript/^ result /(?P [^ // ] +)/ ^(?Presultshandler)/ schema/$ [name ='api_get_schema'] ^ melotranscript/^ result /(?P [^ //] +)/ ^(?Presultshandler)/ set /(?P\w [\ w /; - ]*)/ $ [name ='api_get_multiple'] ^ melotranscript/^ result /(?P [^ //] +)/ ^(?Presultshandler)/(?P\w [\ w/- ]*)/ $ [name ='api_dispatch_detail'] …
如何配置Tastypie将字段视为唯一?如果我尝试为标记为唯一的字段插入重复条目,我的期望是接收某种非500错误(可能是409冲突?)作为响应.
我查看了文档,看起来它应该对我来说很明显,但由于某种原因,我没有得到我期望看到的响应.
这是文档链接:
http://readthedocs.org/docs/django-tastypie/en/latest/fields.html?highlight=unique
示例代码如下:
urls.py
v1_api = Api(api_name='v1')
v1_api.register(CompanyResource())
urlpatterns = patterns('',
(r'^api/', include(v1_api.urls)),
)
Run Code Online (Sandbox Code Playgroud)
resource.py
class CompanyResource(ModelResource):
CompanyName = fields.CharField(attribute='company_name')
CompanyId = fields.CharField(attribute='company_id', unique=True)
Contact = fields.CharField(attribute='contact')
Email = fields.CharField(attribute='email')
Phone = fields.CharField(attribute='phone')
class Meta:
queryset = Company.objects.all()
authentication = BasicAuthentication()
authorization = Authorization()
allowed_methods = ['get', 'post']
Run Code Online (Sandbox Code Playgroud)
models.py
class Company(models.Model):
company_name = models.TextField(default=None, blank=True, null=True)
company_id = models.CharField(default='', unique=True, db_index=True, max_length=20)
contact = models.TextField(default=None, blank=True, null=True)
email = models.EmailField(default=None, blank=True, null=True)
phone = models.TextField(default=None, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
我收到的错误如下(使用curl来点击我的本地服务): …
假设我有Django模型的Category和Item,其中Item具有ForeignKey朝向Category(这个字段被命名为category),我在Backbone中创建了Item模型,如下所示:
defaults:{ name:'', category_id:''}
Run Code Online (Sandbox Code Playgroud)
但是当我保存模型一个项目我得到错误:
raise self.field.rel.to.DoesNotExist\n\nDoesNotExist\n
在django/db/models/fields/related.py中,似乎category_id字段不被识别为Item模型的字段.
我正在使用tastyapi,而ItemModel和ItemResource是:
class Item(models.Model):
id = models.AutoField(primary_key=True)
category=models.ForeignKey('categories.Category')
name = models.CharField(max_length=300)
class ProductResource(ModelResource):
category=fields.ForeignKey(CategoryResource,'category')
class Meta:
queryset= Product.objects.all()
resource_name='product'
authorization= Authorization()
Run Code Online (Sandbox Code Playgroud)
一些细节:当related.py文件执行val = getattr(instance, self.field.attname)self.field.attname是STORE_ID但isntance.store_id是无,即使骨干模型,为STORE_ID值,26.
一些帮助?
我有以下资源:
class MyUserResource(resources.MongoEngineResource):
class Meta:
...
authentication = MyKeyAuthentication()
authorization = ApiKeyAuthorization()
def override_urls(self):
return [...]
Run Code Online (Sandbox Code Playgroud)
所有标准tastypie的API调用都通过身份验证和授权进行路由.但是所有自定义函数/ url(在我的override_urls中)都忽略了auth/auth函数......
有什么想法吗?
编辑:
也许问题是没有调用调度程序.问题仍然是为什么......以及我如何改变这种行为!
我想让我的api给我与tastypie的反向关系数据.
我有两个模型,DocumentContainer和DocumentEvent,它们相关:
DocumentContainer有许多DocumentEvents
这是我的代码:
class DocumentContainerResource(ModelResource):
pod_events = fields.ToManyField('portal.api.resources.DocumentEventResource', 'pod_events')
class Meta:
queryset = DocumentContainer.objects.all()
resource_name = 'pod'
authorization = Authorization()
allowed_methods = ['get']
def dehydrate_doc(self, bundle):
return bundle.data['doc'] or ''
class DocumentEventResource(ModelResource):
pod = fields.ForeignKey(DocumentContainerResource, 'pod')
class Meta:
queryset = DocumentEvent.objects.all()
resource_name = 'pod_event'
allowed_methods = ['get']
Run Code Online (Sandbox Code Playgroud)
当我点击我的api url时,我收到以下错误:
DocumentContainer' object has no attribute 'pod_events
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
谢谢.
你如何在api中包含相关字段?
class Foo(models.Model):
name = models.CharField(...)
class Bar(models.Model):
foo = models.ForeignKey(Foo)
description = models.CharField()
Run Code Online (Sandbox Code Playgroud)
每个Foo都有几个与他有关的Bar,比如图像或者什么.
如何在Foo的资源中显示这些Bar?
与tastypie它的退出简单,我不确定Django Rest Framework ..
我试图在这里使用这个例子:http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources
出于某种原因,我得到:
cached_obj_get()只需要2个参数(给定1个)
即使我明确地用2个参数调用它(与上述示例完全相同.这是我的代码:
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/feed%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_feed'), name="api_get_feed"),
]
def get_feed(self, request, **kwargs):
try:
obj = self.cached_obj_get(request=request, **self.remove_api_resource_names(kwargs))
except ObjectDoesNotExist:
return HttpGone()
except MultipleObjectsReturned:
return HttpMultipleChoices("More than one resource is found at this URI.")
feed_resource = FeedItemResource()
return feed_resource.get_list(request, p_id=obj.id)
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Django安装tastypie.我也安装了South.但是当我迁移时,我得到一些奇怪的类型错误.
./manage.py migrate tastypie
Running migrations for tastypie:
- Migrating forwards to 0002_add_apikey_index.
> tastypie:0001_initial
TypeError: type() argument 1 must be string, not unicode
Run Code Online (Sandbox Code Playgroud)
我查看了迁移0002,甚至没有调用类型!
tastypie ×10
django ×9
python ×4
api ×2
backbone.js ×1
foreign-keys ×1
model ×1
python-2.7 ×1
sql ×1
url ×1