标签: tastypie

使用tastypie从PointField返回纬度和经度值

使用django-tastypie v0.9.11 django 1.4.1geodjango.

在geodjango之前,我曾经将我的lat和lng值直接保存到我的模型中.然后,当我调用API时,我只是轻松地提取我的值.像这样的东西:

{
    "id": "1",
    "lat": "-26.0308215267084719",
    "lng": "28.0101370772476450",
    "author": "\/api\/v1\/user\/3\/",
    "created_on": "2012-07-18T14:33:31.081105",
    "name": "qweqwe",
    "updated_on": "2012-09-06T14:17:01.658947",
    "resource_uri": "\/api\/v1\/spot\/1\/",
    "slug": "qweqwe"
},
Run Code Online (Sandbox Code Playgroud)

现在我已经升级了我的webapp以使用geodjango,现在我将我的信息存储在PointField()中.现在,如果我对我以前制作的API进行相同的调用,我会回复此:

{
    "id": "1",
    "point": "POINT (28.0101370772476450 -26.0308215267084719)",
    "author": "\/api\/v1\/user\/3\/",
    "created_on": "2012-07-18T14:33:31.081105",
    "name": "qweqwe",
    "updated_on": "2012-09-06T14:17:01.658947",
    "resource_uri": "\/api\/v1\/spot\/1\/",
    "slug": "qweqwe"
},
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,点值不同,因此我的移动应用程序正在破碎.

我的问题是如何从点数字段中获取纬度和经度值,并像以前一样使用查询集返回它们?

python api django geodjango tastypie

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

Django TastyPie,如何在POST后使用ManyToMany字段触发操作?

我有一个使用Django和TastyPie构建的REST API.我的目标是在将新数据发布到特定模型时将任务添加到我的作业队列中.

我打算挂钩到post_save然后触发,但模型包含ManyToMany关系,因此在m2m关系更新之前触发post_save并挂钩到m2m_changed信号似乎很乱.我收到多个信号事件,我的代码需要在每个事件之后检查实例并尝试确定它是否准备好触发事件.一些ManyToMany字段可以是Null,所以当我得到m2m_changed信号时,我真的不知道它是否已经完成保存.

有没有正确的方法来做到这一点?TastyPie允许我挂钩POST事件并在最后做一些事情吗?我发现的所有东西都指向post_save事件来做这件事.

当完成给定模型实例的所有m2m更新时,Django是否有办法发信号通知我?

django tastypie

4
推荐指数
2
解决办法
1449
查看次数

PUT请求django tastypie资源不起作用

我正在尝试对我的django tastypie资源执行put请求以更新用户信息.到目前为止,我可以发帖请求,但放不起作用.

在我的api.py我有这个:

class UserResource(ModelResource):
   class Meta:
       queryset = User.objects.all()
       resource_name = 'auth/user'
       fields = ['username', 'email']
       authentication = BasicAuthentication()
       authorization = DjangoAuthorization()
       filtering = {
           "username": ('exact',),
       }

class UserSignUpResource(ModelResource):
   class Meta:
       object_class = User
       resource_name = 'register_user'
       fields = ['username', 'email' , 'password']
       allowed_methods = ['put','post']
       authentication = BasicAuthentication()
       authorization = DjangoAuthorization()
       queryset = User.objects.all()


    def obj_create(self, bundle, request=None, **kwargs):

         bundle = super(UserSignUpResource, self).obj_create(bundle)
Run Code Online (Sandbox Code Playgroud)

并测试我使用这个:

import urllib2, json

def post():

    def basic_authorization(user, password):
       s = user + ":" …
Run Code Online (Sandbox Code Playgroud)

python django rest put tastypie

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

从 tastypie 上的资源中删除列表端点

我的 api 上有一个资源,它总是返回登录的用户。该资源是只读的。我希望列表 uri 充当详细 uri,并删除详细 url。

因此,/api/v1/user/将返回登录的用户,任何其他 url 都会失败。这就是我为实现这一目标所做的事情:

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        fields = ['email', 'name']
        authentication = MultiAuthentication(SessionAuthentication(), BasicAuthentication())
        authorization = Authorization()
        list_allowed_methods = []
        detail_allowed_methods = ['get']

    def base_urls(self):
        '''
        The list endpoint behaves as the list endpoint.
        '''
        return [
            url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
            url(r"^(?P<resource_name>%s)/schema%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_schema'), name="api_get_schema")
        ]

    def obj_get(self, bundle, **kwargs):
        '''
        Always returns the logged in user.
        '''
        return bundle.request.user

    def get_resource_uri(self, bundle_or_obj=None, …
Run Code Online (Sandbox Code Playgroud)

python rest tastypie

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

为什么我的tastypie缓存没有被调用?

我正在查看tastypie缓存文档并尝试设置我自己的简单缓存,但缓存似乎没有被调用.当我访问的http://本地主机:8000/API /调查/格式= JSON,我让我的tastypie产生JSON,但我没有得到从缓存类的输出.

from tastypie.resources import ModelResource
from tastypie.cache import NoCache
from .models import Poll


class JSONCache(NoCache):
    def _load(self):
        print 'loading cache'
        data_file = open(settings.TASTYPIE_JSON_CACHE, 'r')
        return json.load(data_file)

    def _save(self, data):
        print 'saving to cache'
        data_file = open(settings.TASTYPIE_JSON_CACHE, 'w')
        return json.dump(data, data_file)

    def get(self, key):
        print 'jsoncache.get'
        data = self._load()
        return data.get(key, None)

    def set(self, key, value, timeout=60):
        print 'jsoncache.set'
        data = self._load()
        data[key] = value
        self._save(data)


class PollResource(ModelResource):
    class Meta:
        queryset = Poll.objects.all()
        resource_name = …
Run Code Online (Sandbox Code Playgroud)

python django tastypie

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

在django-tastypie中,可以在模式中显示选择吗?

我试图弄清楚我是否可以choices向使用好吃的API的客户表示模型字段。

我有一个django(1.4.1)应用程序,正在为其实现django-tastypie(0.9.11)API。我有一个类似于以下内容的Model和ModelResource:

class SomeModel(models.Model):
    QUEUED, IN_PROCESS, COMPLETE = range(3)

    STATUS_CHOICES = (
        (QUEUED, 'Queued'),
        (IN_PROCESS, 'In Process'),
        (COMPLETE, 'Complete'),
    )

    name = models.CharFIeld(max_length=50)
    status = models.IntegerField(choices=STATUS_CHOICES, default=QUEUED)

class SomeModelResource(ModelResource):
    class Meta:
        queryset = SomeModel.objects.all()
        resource_name = 'some_model'
Run Code Online (Sandbox Code Playgroud)

当我查看API中的对象时,名称和状态字段显示如下:

{
    ...
    "objects":[
    {
        "name": "Some name 1",
        "status": 0
    },
    {
        "name": "Some name 2",
        "status": 2
    }]
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以SomeModelResource使用水合/脱水方法进行更改以显示状态的字符串值,如下所示,这对于客户来说将具有更多价值:

{
    ...
    "objects":[
    {
        "name": "Some name 1",
        "status": "Queued"
    },
    {
        "name": "Some name 2",
        "status": …
Run Code Online (Sandbox Code Playgroud)

python api django tastypie

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

Django Tastypie - 仅限对象详细信息的资源

在使用Tastypie的Django中,有没有办法配置资源,只显示对象详细信息?

我想要一个url /user,它返回经过身份验证的用户的详细信息,而不是包含单个用户对象的列表.我不想用来/users/<id>获取用户的详细信息.

这是我的代码的相关部分:

from django.contrib.auth.models import User
from tastypie.resources import ModelResource

class UserResource(ModelResource):

    class Meta:
        queryset        = User.objects.all()
        resource_name   = 'user'
        allowed_methods = ['get', 'put']
        serializer      = SERIALIZER      # Assume those are defined...
        authentication  = AUTHENTICATION  # "
        authorization   = AUTHORIZATION   # "

    def apply_authorization_limits(self, request, object_list):
        return object_list.filter(pk=request.user.pk)
Run Code Online (Sandbox Code Playgroud)

python django rest tastypie

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

如何改变tastypie时间格式

我正在使用Django 1.4.2和Tastypie 0.9.11编写API服务器.

对于所有日期时间输出,我使用默认的iso 8601格式,例如:"2012-11-20T02:48:19 + 00:00".但我希望获得"2012-11-20T02:48:19Z"格式.如何在不自定义每个日期时间字段的情况下轻松完成?

django tastypie

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

Django obj_create在我的表单验证之前运行

在TastyPie中,obj_create在我的表单验证之前运行,它似乎被跳过,为什么?

我的代码

class AccountCreateResource(ModelResource):
    class Meta:
        queryset = CompanyUser.objects.all()
        resource_name = 'accounts/create'
        allowed_methods = ['post']
        validation = FormValidation(form_class=UserCreationForm)

    def obj_create(self, bundle, request=None, **kwargs):

        CompanyUser.objects.create_user(email=bundle.data['email'],
                                            company=bundle.data['company'],
                                            password=bundle.data['company'])
Run Code Online (Sandbox Code Playgroud)

python django tastypie

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

'用户'对象没有属性'用户名'

我有我的用户模型(AbstractBaseUserDjango 1.5)我使用电子邮件作为用户名进行身份验证,并ModelResource为我的API提供以下内容

class CartItemResource(ModelResource):
    product = fields.ForeignKey(CartItemRelatedResource, 'product', full=True)

    class Meta:
        queryset = CartItem.objects.all()
        resource_name = 'cart_item'
        excludes = ['creation_date', 'modification_date']
        allowed_methods = ['post', 'get', 'delete']
        authorization = CartAuthorization()
        authentication = SessionAuthentication()
Run Code Online (Sandbox Code Playgroud)

当向API发出GET请求时,我得到:

'用户'对象没有属性'用户名'

编辑用户模型:

class User(AbstractBaseUser):
    objects = UserManager()
    name = models.CharField(max_length=100)
    email = models.EmailField(
        max_length=255,
        unique=True,
    )
    phone = models.IntegerField(max_length=10, null=True)
    is_admin = models.BooleanField(default=False, blank=True)
    is_driver = models.BooleanField(default=False, blank=True)
    lastOrderID = models.CharField(max_length=25, null=True)

    USERNAME_FIELD = 'email'
    #REQUIRED_FIELDS = ['name','phone']
    REQUIRED_FIELDS = ['name']

    def …
Run Code Online (Sandbox Code Playgroud)

django django-models tastypie

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

标签 统计

tastypie ×10

django ×9

python ×7

rest ×3

api ×2

django-models ×1

geodjango ×1

put ×1