标签: tastypie

tastypie:PUT不起作用:从请求的数据流中读取后,错误无法访问正文

我正在浏览器中使用REST客户端尝试django-tastypie(Postman)

GET运作良好:

GET http://127.0.0.1:8000/api/v1/entry/
GET http://127.0.0.1:8000/api/v1/entry/1/
Run Code Online (Sandbox Code Playgroud)

但我不能让PUT使用条目:

PUT GET http://127.0.0.1:8000/api/v1/entry/1/
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

{
    "error_message": "You cannot access body after reading from request's data stream",
    ...
}
Run Code Online (Sandbox Code Playgroud)

我允许在资源中使用该方法.哪里可以来的?

谢谢

django tastypie

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

如何检查骨干中模型的特定字段中是否存在数据?

我有一个模特.我注意到我添加到该模型.没有问题.

当我实例化视图时,我想看看data.objects是否包含注释中的内容.

我在哪里放if语句?在视图渲染中?我该如何测试呢.

这里有js和骨干菜鸟,所以请原谅我错过了基础知识.

Lemme知道并非常感谢.

欢迎任何对教程的引用.

更新:这是我的模型的视图

var BlasterView = Backbone.View.extend({

    tagName: 'li',
    className: 'blaster',

    events: {

        'click .td-blaster a': 'done'

    },

    initialize: function() {

        _.bindAll(this, 'render');

    },

    render: function(){

        this.$el.html(ich.blasterTemplate(this.model.toJSON()));
        return this;

    },

    done: function(){

        this.model.toggle();

        this.$el.animate({
            backgroundColor:'#faeca9'
        }, 600 ).delay(600).fadeOut('fast');

        return false;

    }

});
Run Code Online (Sandbox Code Playgroud)

backbone.js tastypie

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

如何在tastypie中创建自定义嵌套ToMany资源?

我在django中有一个稍微复杂的模型结构,其中包含一个UserProfile

class UserProfile(models.Model):
    shoppinglist = models.ManyToManyField(Offer)
    user         = models.OneToOneField(User)
    follows = models.ManyToManyField('self', related_name='followers', symmetrical=False, blank=True)
Run Code Online (Sandbox Code Playgroud)

和报价

class Offer(models.Model):
    description = models.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud)

这意味着每个用户都可以关注其他用户,并可以使用他们喜欢的优惠创建购物清单.

我可以创建一个查询,以查看优惠是否在我所关注的任何人的购物清单中

profile.follows.filter(shoppinglist=offer)
Run Code Online (Sandbox Code Playgroud)

并返回UserProfile对象的查询集.

现在,我在我的tastypie API中使用了OfferResource和UserProfileResource来代表这两个模型.

我需要做的是,每当我收到一份优惠清单时,都会在每个优惠中添加一个自定义字段,其中包含我所关注的"喜欢"优惠的人员列表.

offer: {
    description: 'Something'
    liked_by: [
        { ... },
        { ... }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以覆盖'dehydrate'方法来添加自定义字段但是如果我添加

def dehydrate(self, bundle):
    bundle.data['liked_by'] = profile.follows.filter(shoppinglist=bundle.obj)
Run Code Online (Sandbox Code Playgroud)

在likes_by字段中生成的用户列表未被序列化或脱水.

有任何想法吗?

django tastypie

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

Tastypie:不要发送整个uri资源(只是id)

我开始将Tastypie添加到我正在开发的一个非常小的Django应用程序中,我想知道是否有一种方法只发送一个关系所指向的资源的数字id,而不是资源所在的uri.

例如,使用文档中提供的示例之一:

暴露的"Entry"资源如下所示:

{
    "body": "Welcome to my blog!",
    "id": "1",
    "pub_date": "2011-05-20T00:46:38",
    "resource_uri": "/api/v1/entry/1/",
    "slug": "first-post",
    "title": "First Post",
    "user": "/api/v1/user/1/"
}
Run Code Online (Sandbox Code Playgroud)

它与"用户"的关系显示为 "user": "/api/v1/user/1/".有没有任何方法可以制作它"user": 1(整数,如果可能的话),所以它看起来像以下?

{
    "body": "Welcome to my blog!",
    "id": "1",
    "pub_date": "2011-05-20T00:46:38",
    "resource_uri": "/api/v1/entry/1/",
    "slug": "first-post",
    "title": "First Post",
    "user": 1
}
Run Code Online (Sandbox Code Playgroud)

我喜欢这个想法或者保持resource_uri属性是完整的,但是当涉及到建模Sql关系时,我宁愿只有id(或者数字id的列表,如果关系是" ToMany ").dehydrate_userEntryResource类添加方法是否是个好主意?它似乎工作,但也许有一种更通用的方式(避免必须dehydrate为每个关系编写一个方法)

先感谢您

python formatting tastypie

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

是否有Django和Tastypie的身份验证示例?

Django和Tastypie有基本的身份验证示例吗?我对Django中的身份验证是如何工作有点困惑,特别是对于Tastypie.我想知道身份验证如何与api密钥一起工作以及如何使用Django内置的User模型对用户进行身份验证.任何建议或代码都非常感谢.谢谢.

django django-models django-authentication tastypie

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

django tastypie错误"没有这样的表"

我有一个使用tastypie的Django项目.当我尝试从auth_user中删除一行时,如下所示:

user.delete()
Run Code Online (Sandbox Code Playgroud)

它会引发错误

DatabaseError: (1146, "Table 'develop.tastypie_apikey' doesn't exist")
Run Code Online (Sandbox Code Playgroud)

我搜索了一会儿,并且知道当你创建一个新行时会发生这种情况,但我想删除.

有人知道此时可能出现的问题吗?

非常感谢!

django tastypie

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

Tastypie建筑捆绑与字典

根据手册应该是可能的

给定一个对象,一个数据字典或两者,构建一个Bundle,用于整个脱水/水合循环.

所以我使用Resource将它们放在一起

def get_list(self, request, **kwargs):

    bundles = []

    foo = {}
    foo['bar'] = 1

    bundle = self.build_bundle(data=foo, request=request)
    bundles.append(self.full_dehydrate(bundle))

    serialized = {}
    serialized[self._meta.collection_name] = bundles
    serialized = self.alter_list_data_to_serialize(request, serialized)
    return self.create_response(request, serialized)
Run Code Online (Sandbox Code Playgroud)

但即使我正在通过它,full_hydrate()bar也会窒息.我在这里错过了什么?

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
  77.         return view_func(*args, **kwargs)
File "/home/dan/project/tastypie/resources.py" in wrapper
  203.                 response = callback(request, *args, **kwargs)
File "/home/dan/project/tastypie/resources.py" in dispatch_list
  445.         return self.dispatch('list', request, **kwargs)
File …
Run Code Online (Sandbox Code Playgroud)

tastypie

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

如何根据位置坐标过滤django查询集?

假设我有一个照片模型.在照片模型中,我的照片模型中有经度和纬度字段.

 class Photo(models.Model):
      photographer = models.ForeignKey(Photographer, related_name = 'shot_owner')
      title = models.CharField(max_length=140, blank=True)
      description = models.CharField(max_length, blank=True)
      longitude = models.DecimalField(max_digits=16, decimal_places = 14, null=True, blank=True)
      latitude = models.DecimalField(max_digits=16, decimal_places = 14, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)

我使用Django Tastypie作为我的休息框架.假设用户决定他们想要看到半径10公里范围内的所有照片.怎么能实现这个目标?以下是我的资源:

class PhotosNearMe(ModelResource):
photographer = fields.ForeignKey(PhotographerResource, 'photographer', full=True)
class Meta:
    queryset = Photo.objects.all()
    resource_name = 'photos-near-me'
    fields = ['id', 'title', 'description', 'latitude','longitude','photographer']
    authentication = BasicAuthentication()
    authorization = DjangoAuthorization()
    serializer = Serializer(formats=['json'])
    include_resource_uri = False
    filtering = {
            'photographer' : ALL_WITH_RELATIONS,

}

def get_object_list(self, request):
        return super(PhotosNearMe, …
Run Code Online (Sandbox Code Playgroud)

python django tastypie

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

Tastypie从DELETE请求中返回数据吗?

我有一个简单的资源,我想执行DELETE。成功后,我想获取已删除对象的ID。根据文档,always_return_data- 指定所有HTTP方法(DELETE除外)应返回数据的序列化形式。

http://django-tastypie.readthedocs.org/en/latest/resources.html#always-return-data

class SimpleResource(resources.MongoEngineResource):
    class Meta:
        queryset = Simple.objects.all()
        resource_name = 'simple'
        allowed_methods = ('get', 'put', 'post', 'delete', 'patch')
        always_return_data = True
Run Code Online (Sandbox Code Playgroud)

问题: 是否有序列化数据以返回已删除的对象?

django mongodb tastypie

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

覆盖网址错误:未定义全局名称“网址”

我打算覆盖url以针对不同类型的url调用特定方法。下面的代码

Resources.py

class LCUserResource(ModelResource):
    class Meta:
        queryset = LCUser.objects.all()
        resource_name= 'lcuser'
        authorization = Authorization()

def override_urls(self):
    return [
        url(r'^register/'%
            (self._meta.resource_name, trailing_slash()), self.wrap_view('register_user'), name="api_register_user"),
    ]
Run Code Online (Sandbox Code Playgroud)

urls.py

v1_api = Api(api_name='v1')
v1_api.register(LCUserResource())

urlpatterns = [
    url(r'^api/', include(v1_api.urls)),
]
Run Code Online (Sandbox Code Playgroud)

我正在尝试通过http:// localhost:8000 / api / v1 / lcuser / register /访问api

但是我收到错误,未定义全局名称url。

我尝试从django.conf.urls.defaults导入*

然后我得到没有名为默认模块的模块

django tastypie

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