标签: tastypie

使用 Django 和 Tastypie 返回随机项目

在直接的 Django 中,您可以通过以下方式访问随机模型实例:

randinst = MyModel.objects.order_by('?')
Run Code Online (Sandbox Code Playgroud)

注意:虽然这存在性能问题,但我已经使用 sqlite 后端进行了测试,并且在多达 100000 次尝试中确实得到了非常随机的结果。由于我的应用程序不需要除此之外的显着性能,因此我不关心其他后端。

我希望完成的是:一个客户端发出一个请求,/api/v1/mymodel/?limit=10然后通过tastypie 从MyModel 中获取一组随机的10 行,就像您将上面的代码片段运行10 次一样。然后它发出相同的请求,并接收 10 个不同的(在概率范围内)随机行。

注意:我试过请求/api/v1/mymodel/?ordering='?'和所有合理的变体都无济于事。设置也无济于事MyModelResource.Meta.ordering = ['?']

有什么办法可以用美味派来实现我的目标吗?还有其他解决方案可以尝试吗?谢谢。

random django tastypie

2
推荐指数
1
解决办法
402
查看次数

如何为tastypie设置授权标头?

在请求中传递值作为参数时,它可以工作:

curl "http://localhost:8080/wordgame/api/v1/rounds/?username=test_user&api_key=12345678907a9cb56b7290223165e0a7c23623df&format=json"
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将值作为标头传递时,它不起作用.这导致401:

curl -H "Authorization: ApiKey test_user:12345678907a9cb56b7290223165e0a7c23623df" -H "Accept: application/json" http://localhost:8080/wordgame/api/v1/rounds/
Run Code Online (Sandbox Code Playgroud)

我正在使用Tastypie ApiKeyAuthentication

python django curl tastypie

2
推荐指数
1
解决办法
1811
查看次数

django tastypie 401未经授权

我不能让这个为我的生活工作.

我在api.py中有这个

class catResource(ModelResource):
    class Meta:
        queryset = categories.objects.all()
        resource_name = 'categories'
    allowed_methods = ['get', 'post', 'put']
    authentication = Authentication()
Run Code Online (Sandbox Code Playgroud)

所以,当我尝试:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name":"Test", "parent": "0", "sort": "1","username":"admin","password":"password"}' http://192.168.1.109:8000/api/v1/categories/
Run Code Online (Sandbox Code Playgroud)

我明白了:

HTTP/1.0 401 UNAUTHORIZED
Date: Sat, 21 Sep 2013 10:26:00 GMT
Server: WSGIServer/0.1 Python/2.6.5
Content-Type: text/html; charset=utf-8
Run Code Online (Sandbox Code Playgroud)

该模型:

class categories(models.Model):
    name = models.CharField(max_length=255)
    parent = models.ForeignKey('self', blank=True,null=True)
    sort = models.IntegerField(default=0)


    def __unicode__(self):
        if self.parent:
            prefix = str(self.parent)
        else:
            return self.name
        return ' > '.join((prefix,self.name)) …
Run Code Online (Sandbox Code Playgroud)

django django-models tastypie

2
推荐指数
1
解决办法
4020
查看次数

如何在tastypie中使用DjangoAuthorization()来限制对资源的GET访问

我正在使用tastypie来创建RESTful API.我根据django管理员权限限制了用户授权. 根据文档,我正在尝试实现DjangoAuthorization().

class myResource(ModelResource):
   class Meta:
      queryset = myModel.objects().all()
      allowed_methods = ['get','post']
      authentication = ApiKeyAuthentication()
      authorization = DjangoAuthorization()
Run Code Online (Sandbox Code Playgroud)

目前,fakeuser完全没有Django权限的用户myModel仍然可以从api获取数据.该用户被适当地限制POST数据.

tl; dr如何扩展DjangoAuthorization()类以限制模型上没有Django权限的用户的GET

python django tastypie

2
推荐指数
1
解决办法
838
查看次数

从外部脚本导入 Django 设置

我的 Django 项目中有一个 python 脚本,旨在与 Django 应用程序分开运行。我想在我的 Django 应用程序上使用 settings.py 我该怎么做。

当我尝试导入时

from django.conf import settings
Run Code Online (Sandbox Code Playgroud)

我明白了

ImportError: No module named DjangoTastypie.settings
Run Code Online (Sandbox Code Playgroud)

我的项目结构

在此处输入图片说明

我正在使用 eclipse-> 作为 python 运行

python django tastypie

2
推荐指数
1
解决办法
2261
查看次数

Django Tastypie,ManyToMany保存错误

当我通过tastypie api保存项目时,我遇到了问题.(POST方法)

这是我的api.py代码.

from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie.authorization import DjangoAuthorization
from tastypie.authentication import BasicAuthentication
from tastypie import fields
from apps.clients.models import Client
from django.contrib.auth.models import User

class ClientAPI(ModelResource):
    users = fields.ToManyField('apps.clients.api.ClientUserAPI', 'users',related_name='entry',full=True)


    class Meta:
        queryset = Client.objects.all()
        resource_name="clients"
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()
        filtering={
            "users":ALL
        }

    def hydrate_m2m(self,bundle):
        if bundle.data.get("users"):
            for user_id in bundle.data["users"]:
                new_user = User.objects.get(id=user_id)
                bundle.obj.users.add(new_user)


class ClientUserAPI(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'users'
        fields = ['username', 'first_name', 'last_name', 'last_login']
        authentication = …
Run Code Online (Sandbox Code Playgroud)

api django many-to-many django-orm tastypie

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

Django Tastypie没有使用ManyToManyField更新资源

为什么带有此PUT请求的ManyToManyField资源不会更新?

curl --dump-header - -H "Content-Type: application/json" -X PUT --data '{"uuid":"blah","pass_token":"blah","favorites": ["/api/v1/organizations/1/"]}' http://localhost:8000/api/v1/devices/2/
Run Code Online (Sandbox Code Playgroud)

我收到了这个回复:

HTTP/1.0 400 BAD REQUEST
Date: Wed, 11 Jul 2012 22:21:15 GMT
Server: WSGIServer/0.1 Python/2.7.2
Content-Type: application/json; charset=utf-8

{"favorites": ["\"/api/v1/organizations/1/\" is not a valid value for a primary key."]}
Run Code Online (Sandbox Code Playgroud)

这是我的资源:

class OrganizationResource(ModelResource):
    parent_org = fields.ForeignKey('self','parent_org',null=True, full=True,blank=True)

    class Meta:
        allowed_methods = ['get',]
        authentication = APIAuthentication()
        fields = ['name','org_type','parent_org']
        filtering = {
            'name': ALL,
            'org_type': ALL,
            'parent_org': ALL_WITH_RELATIONS,
        }
        ordering = ['name',]
        queryset = Organization.objects.all()
        resource_name = 'organizations'

class …
Run Code Online (Sandbox Code Playgroud)

python django m2m tastypie

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

更新时的Tastypie错误 - 字段已被赋予不是URI的数据,而不是字典相似且没有'pk'属性

当我尝试更新资源时,我不断收到此错误.

我想要更新的资源称为Message.它有一个外键帐户:

class AccountResource(ModelResource):
    class Meta:
        queryset = Account.objects.filter()
        resource_name = 'account'
        '''
        set to put because for some weird reason I can't edit 
        the other resources with patch if put is not allowed.
        '''
        allowed_methods = ['put']
        fields = ['id']


    def dehydrate(self, bundle):
        bundle.data['firstname'] = bundle.obj.account.first_name   
        bundle.data['lastname'] = bundle.obj.account.last_name
        return bundle
Run Code Online (Sandbox Code Playgroud)

class MessageResource(ModelResource):
    account = fields.ForeignKey(AccountResource, 'account', full=True)

    class Meta:
        queryset = AccountMessage.objects.filter(isActive=True)
        resource_name = 'message'
        allowed = ['get', 'put', 'patch', 'post']
        authentication = MessageAuthentication()
        authorization = MessageAuthorization()   
        filtering …
Run Code Online (Sandbox Code Playgroud)

api django rest tastypie

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

什么是返回JSON对象的TastyPie资源和Django视图之间的区别?

当我可以从Django视图返回JSON时,为什么我要使用TastyPie?

django tastypie

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

REST应用程序架构

我正在开发一个RESTful应用程序,其目标是允许用户跟踪他在体育活动中的结果,进度和表现.

我想为此应用创建两个或更多客户端:

1)网络客户端

2)移动客户端(iOS/Android)

我正在使用tastypie应用程序在django中编写它,我想知道我是否应该在同一个应用程序中创建Web客户端,这将提供RESTful api或者我应该将其作为纯REST服务并构建单独的Web客户端,它将通过它来联系它api?

至于现在,我没有看到将两者结合在一起的任何缺点,但我不是具有这种架构的程序的专家,所以我正在寻找一些建议背后的论证.

architecture django rest tastypie

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