标签: tastypie

Ajax POST和Django Tastypie

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"latlong": "test"}' http://localhost:8000/geo/api/geolocation/
Run Code Online (Sandbox Code Playgroud)

以上工作正常,但当我尝试在下面的ajax中复制POST时,我得到500错误.

$.ajax({
  type: 'POST',
  url: 'http://localhost:8000/geo/api/geolocation/',
  data: '{"latlong": "test"}',
  success: latlongSaved(),
  dataType: "application/json",
  processData:  false,
});
Run Code Online (Sandbox Code Playgroud)

错误信息是:

{"error_message": "The format indicated 'application/x-www-form-urlencoded' had no available deserialization method. Please check your ``formats`` and ``content_types`` on your Serializer." .... }
Run Code Online (Sandbox Code Playgroud)

值得注意的是这是跨域的,我正在使用通过git:gist找到的django-crossdomainxhr-middleware.py

如果我向ajax调用添加内容类型,如下所示:

contentType: "application/json"
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

XMLHttpRequest cannot load http://localhost:8000/geo/api/geolocation/. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Request URL:http://localhost:8000/geo/api/geolocation/
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Access-Control-Request-Headers:Origin, Content-Type, …
Run Code Online (Sandbox Code Playgroud)

django ajax jquery tastypie

8
推荐指数
2
解决办法
7483
查看次数

需要一个使用django-tastypie进行授权的示例

我对Django和它的生态系统相对较新.我正在使用django-tastypie为我们的移动客户端编写REST api.我已经浏览了网上几乎所有关于如何使用tastypie创建REST接口的例子.但是它们都不是特定于从客户端发布数据以及如何授权客户端.

我使用了示例中显示的tastypie.authentication.BasicAuthentication.它打开一个弹出窗口询问用户名和密码,并在浏览器上正常工作.但我不确定,它是否会在移动设备上做同样的事情(具体来说,本机IOS应用程序).当用户没有使用浏览器而是使用本机应用程序时,如果用户将请求登录如何在他/她的移动设备上显示此弹出窗口,我就不太了解.

我完全迷失了,我真的很感激你的帮助.

django tastypie

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

在Django Tastypie API中包含子资源

我正在计划一个用于REST API的Django和Tastypie站点,而且我很难找到将子资源包含在返回资源中的"正确"方法.

作为沙盒,我使用Ticket模型和TicketComment模型制作了一个小应用程序,其中注释属于故障单.我查看了嵌套资源上的Tastypie Cookbook配方(http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources),但我很难理解为什么要这样做.下面的代码使用django.forms.models.model_to_dict()来获取票证中的注释,但我认为某处必须有"陷阱".

我不应该做我现在正在做的事情吗?此外,是否有一种更干净的感觉模式比菜谱中列出的更清晰?

型号如下:

# tickets/models.py

from django.db import models

class Ticket(models.Model):
    title = models.CharField(max_length=200)
    create_ts = models.DateTimeField(auto_now_add=True)
    submitter_email = models.EmailField()
    PRIORITY_CHOICES = (
        ('H', 'High'),
        ('M', 'Medium'),
        ('L', 'Low'),)
    priority = models.CharField(max_length=1, choices=PRIORITY_CHOICES)
    description = models.TextField()
    STATUS_CHOICES = (
        ('NEW', 'New & Unclaimed'),
        ('WIP', 'Work In Progress'),
        ('RES', 'Resolved'),
        ('CLS', 'Closed'),)
    status = models.CharField(max_length=3, choices=STATUS_CHOICES)

    def __unicode__(self):
        return "<Ticket:%d:%s>" % (self.id, self.title,)

class TicketComment(models.Model):
    ticket = models.ForeignKey(Ticket)
    comment_ts = models.DateTimeField(auto_now_add=True)
    commenter_email = models.EmailField()
    comment = models.TextField()

    def …
Run Code Online (Sandbox Code Playgroud)

python api django rest tastypie

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

Tastypie-django自定义错误处理

我想返回一些JSON响应,而不是只返回带有错误代码的标头.在tastypie中是否有办法处理这样的错误?

django error-handling tastypie

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

Heroku和Django有405错误

我试图将我的django项目从apache设置移动到heroku.在这一点上,除了使用PATCH Http方法(我与django-tastypie结合使用)时遇到的问题,一切似乎都运行正常.

我有一个允许这种方法的中间件,它在我以前的apache服务器上工作.现在我得到的是405(METHOD_NOT_ALLOWED)错误.常见的HTTP方法仍然有效(GET,POST,DELETE,POST).我还读过nginx默认不支持OPTIONS(可能还有PATCH请求?),必须进行一些配置.根据我的阅读,nginx为OPTIONS方法请求返回类似的405错误.我也很确定heroku使用nginx前端,所以这可能是问题所在.

这引出了我的下一个问题:我不知道如何添加自己的标题,因为这完全由heroku控制.有谁知道究竟是什么问题?有没有办法,除了放弃PATCH方法(这使事情更方便),让PATCH方法在heroku上工作?

django http heroku http-status-code-405 tastypie

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

Tastypie,为多对多关系添加元素

我正在构建一个django tastypie api,我在ManyToMany关系中添加元素时遇到了问题

示例,models.py

class Picture(models.db):
    """ A picture of people"""
    people = models.ManyToManyField(Person, related_name='pictures',
        help_text="The people in this picture",
    )

class Person(models.db):
    """ A model to represet a person """
    name = models.CharField(max_length=200,
        help_text="The name of this person",
    )
Run Code Online (Sandbox Code Playgroud)

资源:

class PictureResource(ModelResource):
    """ API Resource for the Picture model """
    people = fields.ToManyField(PersonResource, 'people', null=True,
        related_name="pictures", help_text="The people in this picture",
    )
class PersonResource(ModelResource):
    """ API Resource for the Person model """
    pictures = fields.ToManyField(PictureResource, 'pictures', null=True, …
Run Code Online (Sandbox Code Playgroud)

django rest tastypie

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

RESTful,用户身份验证和Django-tastypie

为初学者问题道歉.

关于将RESTful服务与不同用户结合使用的一般方法,我有点困惑.特别是,我主要关注的是开发一个API,我只会通过我编写的各种应用程序,即Web应用程序,以及可能会访问相同数据的一些移动应用程序.

(1)由django-tastypie之类的东西产生的休息API是否适合非公开使用(即,当我只想通过我的应用程序授予对这些数据的访问权限时)?

(2)在创建Restful API的登录访问权限时,我是否为我的网络应用程序的所有用户创建了一个登录名 - 或者我是否为自己和我的Web应用程序创建了一个登录名?我的webapp用户帐户是否应被视为与访问Restful API的帐户不同?

基本上,我想使用Django和django-tastypie创建一个应用程序,它允许用户登录,创建和查看对象,订阅用户并查看他们的对象.我想将Tastypie API用于我自己的javascript目的,以便在我的视图中创建序列化和更新相关数据.这些用户帐户在哪里适合这张图片?谢谢!

authentication django rest tastypie

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

使用tastypie资源将查询集转换为json

我有一个模型的tastypie资源.我还有一个视图,它提供了一个需要序列化并发送到客户端的查询集.我正在寻找一种方法让tastypie资源处理查询集的序列化和脱水.

我看到我可以传递一个对象

[Resource.build_bundle(self, obj=None, data=None, request=None)][1]
Run Code Online (Sandbox Code Playgroud)

创建一个包,然后将包传递给

[Resource.full_dehydrate(self, bundle)][2]
Run Code Online (Sandbox Code Playgroud)

最后打电话

[Resource.serialize(self, request, data, format, options=None)][3]
Run Code Online (Sandbox Code Playgroud)

关于脱水数据.

但我想将完整的查询集转换为json,而不仅仅是单个对象.也许我需要的是一种将完整查询集转换为bundle的方法.

任何帮助表示赞赏!

django tastypie

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

python用内部类动态创建类

I'm using django-tastypie and I need to create classes like this from my django models:

class MyModelResource(ModelResource):
    class Meta:
        queryset = MyModel.objects.all()
        allowed_methods = ['get']
Run Code Online (Sandbox Code Playgroud)

Since I have a lot of models in my django app I don't want to repeat myself, and use type() function instead to create all that resource classes. The problem is I don't know how to deal with this inner "Meta" class.

Can you give me an example of how to dynamically create a class …

python metaprogramming metaclass class tastypie

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

在Django tastypie中禁用分页?

我有一个正在研究的tastypie api,在我的api资源的列表视图中,无论列表中的对象数量多少,我都希望得到整个数据列表而不应用分页.我不需要具有高限制的自定义分页符,我想完全禁用分页.

我可能会修改我的客户端以处理分页(api是从C++ DLL而不是Web浏览器访问所以它有点复杂但可能)但是如果我可以禁用它会更容易.

是否有一个开关来禁用不同资源的paginator,或者可能是一个api wide开关来禁用注册到该api对象的所有资源的分页?

python django pagination tastypie

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