标签: tastypie

Django tastypie:资源显示与列表请求中的详细请求不同

我刚刚开始使用django tastypie,而且我很喜欢它.我的问题:我正在搜索与管理视图中相同的功能:为foreignkey字段指定在其他对象的列表响应中看到的内容以及详细响应中的内容.

让我们说这是我简化的模型:

class Location(models.Model):
    name = models.CharField(max_length=256, blank=True)
    longitude = models.FloatField(blank=True, default=0.0)
    latitude = models.FloatField(blank=True, default=0.0)
    description = models.CharField(max_length=256, blank=True)
    shortname = models.CharField(max_length=256, blank=True)
    tooltiptext = models.CharField(max_length=1000, blank=True)
    locationtype = models.ForeignKey(LocationType, blank=True, null=True)
    public_anonymous = models.BooleanField(default=False, blank=False, null=False)
    public_authorized = models.BooleanField(default=False, blank=False, null=False)
    def __str__(self):
        return '%s' % (self.name)

class Variable(models.Model):
    abbreviation = models.CharField(max_length=64, unique=True)    
    name = models.CharField(max_length=256, blank=True)
    unit = models.CharField(max_length=64, blank=True)
    def __str__(self):
        return '%s  [%s]' % (self.name, self.unit)

class Timeseries(models.Model):
    locationkey = models.ForeignKey(Location)
    variablekey = models.ForeignKey(Variable) …
Run Code Online (Sandbox Code Playgroud)

django tastypie

6
推荐指数
2
解决办法
2774
查看次数

使用Tastypie创建相关资源

我希望tastypie能够创建一个UserProfileResource,因为我发布了一个UserResource.

models.py:

class UserProfile(models.Model):
    home_address = models.TextField()
    user = models.ForeignKey(User, unique=True)
Run Code Online (Sandbox Code Playgroud)

resources.py

class UserProfileResource(ModelResource):
    home_address = fields.CharField(attribute='home_address')

    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'profile'
        excludes = ['id']
        include_resource_uri = False


class UserResource(ModelResource):
    profile = fields.ToOneField(UserProfileResource, 'profile', full=True)
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        allowed_methods = ['get', 'post', 'delete', 'put']
        fields = ['username']
        filtering = {
                'username': ALL,
                }
Run Code Online (Sandbox Code Playgroud)

curl命令:

curl -v -H "Content-Type: application/json" -X POST --data '{"username":"me", "password":"blahblah", "profile":{"home_address":"somewhere"}}' http://127.0.0.1:8000/api/user/
Run Code Online (Sandbox Code Playgroud)

但我得到:

 Django Version:   1.4
 Exception Type: …
Run Code Online (Sandbox Code Playgroud)

django tastypie

6
推荐指数
1
解决办法
6164
查看次数

如何登录?Django TastyPie与ApiKeyAuthentication实际身份验证过程

我有一个Adobe Air移动应用程序,通过TastyPie与Django通信.要使用该应用程序,人们必须先注册.因此,他们必须提供他们的电子邮件和密码.之后他们就可以"登录"了.我认为最好的想法是,在输入成功的用户名/密码组合后,api-key将被发送回移动应用程序,在那里它将被缓存,因此用户"登录".

如果您认为有更好的方式来注册和"登录"用户,请告诉我.

在Django里面我有一个UserRessource类,用于在通过POST发送数据时注册新用户:

class UserResource(ModelResource):
    class Meta:
        allowed_methods = ['get', 'post']
        queryset = User.objects.all()
        resource_name = 'auth'
        authentication = Authentication()
        authorization = Authorization()
        fields = ['username', 'email']

    def obj_create(self, bundle, request=None, **kwargs):
        username, email, password = bundle.data['username'], bundle.data['password'], bundle.data['password'], 
        try:
            bundle.obj = User.objects.create_user(username, email, password)
        except IntegrityError:
            raise BadRequest('That username already exists')
        return bundle
Run Code Online (Sandbox Code Playgroud)

这非常有效.

但现在我正在努力实际登录过程.在我看来,最好通过GET(和https)向这个资源发送用户名和密码,如果这些是有效的,返回用户api密钥.但这可能吗?它干净吗?如果您向该资源发送GET请求,通常TastyPie将显示当前在数据库中的所有用户.但我不需要那些数据,所以我可能会以某种方式覆盖它.我已经检查了http://django-tastypie.readthedocs.org/en/v0.9.9/resources.html但是我没有让它工作.是否有可能覆盖这种行为?

所以实际问题是什么是使用ApiKeyAuthentication"登录"用户的最佳方式? 而且是我的方法正确,干净或者你有更好的方法?你有这种情况下,任何的例子吗?

非常感谢提前!

authentication django login tastypie

6
推荐指数
1
解决办法
2118
查看次数

Tastypie POST位置HTTPS与HTTP

当我将新资源发布到我的RESTful Tastypie API时,我创建了一个资源并得到201响应,如下所示:

HTTP/1.1 201 CREATED
Content-Type: text/html; charset=utf-8
Date: Wed, 19 Sep 2012 01:02:48 GMT
Location: http://example.com/api/v1/resource/12/
Server: gunicorn/0.14.6
Content-Length: 0
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

大!除了我发布到HTTPS URL并希望获得HTTPS Location标头.如何配置tastypie来执行此操作?

加成

我正在使用一些中间件来强制使用SSL,但我不认为这是导致此问题的原因.无论如何它在这里:

class SSLifyMiddleware(object):
    # Derived from https://github.com/rdegges/django-sslify
    def process_request(self, request):
        if not any((not settings.FORCE_SSL, request.is_secure(), request.META.get('HTTP_X_FORWARDED_PROTO', '') == 'https')):
            url = request.build_absolute_uri(request.get_full_path())
            secure_url = url.replace('http://', 'https://')
            return HttpResponseRedirect(secure_url)
Run Code Online (Sandbox Code Playgroud)

加成

这是一个Heroku应用程序.

django https http heroku tastypie

6
推荐指数
1
解决办法
1077
查看次数

Django Tastypie中'obj_get'的正确实现是什么?

我是Django&Tastypie的新手.我想只返回查询中的一个对象.我几乎尝试了一切,似乎无法找到解决方案.这是我的代码如下:

class ProfileResource(ModelResource):
     person = fields.ForeignKey(UserResource, 'user', full=True)

class Meta:
    queryset = Person.objects.all()
    resource_name = 'profile'
    authentication = BasicAuthentication()
    authorization = DjangoAuthorization()
    serializer = Serializer(formats=['json'])
Run Code Online (Sandbox Code Playgroud)

现在我遇到问题的部分是如何使用单个资源返回单个用户对象request.user.

django tastypie

6
推荐指数
1
解决办法
653
查看次数

Django Tastypie缓慢的POST响应

我想实现一个Tastypie资源,允许GET和下面的每用户权限策略POST操作,该模型是非常简单的(类似Tastypie文档中的注意事项型号)和资源本身也很简单,我只是有一个额外的override_urls方法,用Haystack实现搜索.

我现在的主要问题是,尽管在本地运行项目似乎工作正常,但请求速度快,而且一切都很快.一旦我部署了项目(On Linode,使用Nginx,Gunicorn,Runit),我发现POST请求太慢,大约需要1.1分钟才能返回201状态.另一方面,GET请求正如预期的那样运作良好.

我在请求上运行了一个Python Hotshot分析器,它显示整个POST请求占用0.127 CPU秒.我不太确定这里发生了什么.

我应该提一下,我正在为我的Tastypie资源使用ApiKeyAuthentication和DjangoAuthorization.

以下是Chrome Inspector针对请求的屏幕截图:http://d.pr/i/CvCS

如果有人能指引我找到正确的方向来寻找这个问题的答案,那将是很棒的.

谢谢!

编辑:

一些代码:

型号和资源:

class Note(models.Model):
    timestamp = models.DateTimeField('Timestamp')
    user = models.ForeignKey(User)
    page_title = models.CharField("Page Title", max_length=200)
    url = models.URLField('URL', verify_exists=False)
    summary = models.TextField("Summary")
    notes = models.TextField("Notes", null=True, blank=True)

    def __unicode__(self):
        return self.page_title

    def get_absolute_url(self):
        return self.url


class NoteResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')

    class Meta:
        queryset = Note.objects.all()
        resource_name = 'note'
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get']
        always_return_data = True
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization() …
Run Code Online (Sandbox Code Playgroud)

python api django tastypie

6
推荐指数
1
解决办法
996
查看次数

django-tastypie:为什么api密钥有用以及如何支持多个auth方案?

我正在设计一个网站,其中人们将以用户身份登录,并且可能分为多个组,这些组有几种不同的类型.我想要一个人们可以直接使用的网站,以及公开可以被其他网站使用的API.

实现登录系统的最佳方式是什么,该登录系统既适用于网站本身的常规用户,也允许API消费网站代表用户无缝创建帐户,并允许用户查看我的数据网站和API消费网站?

我正在使用Django 1.5,所以我愿意自定义用户模型和所有这些.API将使用Tastypie提供.

编辑:老实说,我的主要问题是我不太了解API密钥何时有用以及它们如何与常规用户登录共存(如果有).

api django django-authentication tastypie

6
推荐指数
1
解决办法
3533
查看次数

没有名为constants的模块

我想用Tastypie框架做示例应用程序.

我将Tastypie添加到已安装的应用程序并urls.py根据需要进行了修改,添加from tastypie.api import Api.但是当我打开时http://localhost:8000/api/v1/?format=json,我得到以下异常:

异常值:没有名为常量的模块

当我跑:

pip install constants
Run Code Online (Sandbox Code Playgroud)

一切看起来都不错

Python 2.6 (and try on 2.7)
Django 1.4
Tastypie 0.10.0
Run Code Online (Sandbox Code Playgroud)

python django tastypie

6
推荐指数
1
解决办法
8154
查看次数

使用localhost webservice连接PhoneGap

连接我的webserwisem PhoneGap(Django + TastyPie)时遇到问题.我的PhoneGap - 版本 - 3.3.0.

使用普通html调用POST或GET - 一切正常当我将项目移动到模拟器时问题开始 - 传输无法以任何方式连接.除了点击链接,"获取页面错误".

我的xml(res/xml/config.xml):

<?xml version="1.0" encoding="utf-8"?>
<!--
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the …
Run Code Online (Sandbox Code Playgroud)

xml django android tastypie cordova

6
推荐指数
1
解决办法
6424
查看次数

批量更新,创建和删除REST方式

使用Tastypie和AngularJS $资源我想执行一组更新,创建和删除操作.

目前,我播放了一个活动:

$rootScope.$broadcast('save');
Run Code Online (Sandbox Code Playgroud)

每个负责使用该$resource服务创建,更新和删除的控制器都会捕获该事件:

ResourceService.update({id:$scope.id}, $scope.element).$promise.then(function(element) {
    $scope.$emit('saved');
});
Run Code Online (Sandbox Code Playgroud)

现在,这会导致客户端和服务器端的某些竞争条件.

在REST方式中以批处理方式执行这组操作的最简单方法是什么?

rest tastypie angularjs angularjs-resource

6
推荐指数
1
解决办法
1940
查看次数