标签: tastypie

Django管理员在安装tastypie后抛出错误?

当我尝试创建一个新用户时,我得到:no such table: tastypie_apikey.有谁知道为什么会这样?tastypie文档没有引用任何需要创建的数据库表,或者确实是一种让tastypie进行任何此类更改的方法.

django django-admin tastypie

3
推荐指数
2
解决办法
1358
查看次数

Django Tastypie包括多对多字段

假设我在Django中有两个模型 - 一个Actor和一个Movie模型,它们之间有很多对很多的关系.现在我已经在Tastypie中为这两个模型定义了API调用,但是当我在一个调用中检索多个电影时,我希望在每个电影中包含Actors的计数,而不需要为每个电影进行额外的API调用在检索到的电影中.

无论如何在Tastypie中指定这个?

谢谢!

django django-models tastypie

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

如何使用Tastypie通过相关模型属性过滤API结果?

鉴于以下API定义,我需要能够按产品别名过滤事件.

鉴于一个事件属于一个Job而一个Job属于一个产品,我不知道如何规范.

api.py:

class ProductResource(ModelResource):

    class Meta:
        queryset = Product.objects.all()
        resource_name = 'product'
        allowed_methods = ['get']
        excludes = ['created_at','updated_at']
        filtering = {
            'alias': ALL
        }

class EnvironmentResource(ModelResource):

    class Meta:
        queryset = Environment.objects.all()
        resource_name = 'environment'
        allowed_methods = ['get']
        excludes = ['created_at','updated_at']

class JobResource(ModelResource):

    product = fields.ForeignKey(ProductResource, 'product')

    class Meta:
        queryset = Job.objects.all()
        resource_name = 'job'
        allowed_methods = ['get']
        excludes = ['created_at','updated_at']

class EventResource(ModelResource):

    environment = fields.ForeignKey(EnvironmentResource, 'environment',full=True)
    job = fields.ForeignKey(JobResource, 'job',full=True)

    class Meta:
        queryset = Event.objects.all()
        resource_name = 'event'
        allowed_methods …
Run Code Online (Sandbox Code Playgroud)

django tastypie

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

获取tastypie中的对象列表(在另一个视图中)

我试图在另一个视图中使用tastypie响应.我已经看过食谱中的食谱了.问题是,我想获得列表视图.在我的情况下,/api/v1/source/.这是我到目前为止所得到的:

sr = SourceResource()
objs = sr.get_object_list(request) # two objects returned
bun = sr.build_bundle(data=objs, request=request)

jsondata = sr.serialize(None, sr.full_dehydrate(bun), 'application/json')
Run Code Online (Sandbox Code Playgroud)

当然这一切都崩溃了.bun.data没有所需的特征(单个对象).那么,有没有人成功完成这项工作?怎么做?

django tastypie

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

反向关系ToManyField Tastypie

我有两个资源(Tastypie),其中一个有一个ToManyField字段:

class SongResource(ModelResource):

    class Meta:
        queryset = Song.objects.all()
        resource_name = 'song'
        authorization = Authorization()



class AlbumResource(ModelResource):
    songs = fields.ToManyField('core.api.SongResource', 'songs', full=True)
    class Meta:
        queryset = Album.objects.all()
        resource_name = 'album'
        authorization = Authorization()
Run Code Online (Sandbox Code Playgroud)

因此,当我访问我的PlaylistResource时,我看到如下内容:

http://127.0.0.1:8000/api/v1/playlist/1/?format=json 以下是我得到的回复:

{
"created_in": "2012-06-24T22:57:01+00:00",
"id": "1",
"number_of_plays": 0,
"playlist_slug": "my-first-playlist",
"playlist_title": "my first playlist",
"resource_uri": "/api/v1/playlist/1/",
"songs": [{
    "genre": "Progressive metal",
    "id": "2",
    "length": "04:19",
    "number_of_plays": 0,
    "price": 0.0,
    "resource_uri": "/api/v1/song/2/",
    "song_slug": "leyla-2008",
    "song_title": "Leyla",
    "song_url": "http://www.amazon.s3.com/prologue",
    "thumbnail": "http://almacosta.files.wordpress.com/2011/05/bob_marley.jpg?w=250",
    "year": 2008
}, {
    "genre": …
Run Code Online (Sandbox Code Playgroud)

django json tastypie

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

如何在django-tastypie上传带有POST请求的文件?

可能重复:
Django-tastypie:POST中文件上传的任何示例?

我目前正在对我的API执行cURL POST请求

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"username":"theusername", "api_key":"anapikey", "video_title":"a title", "video_description":"the description"}' http://localhost:8000/api/v1/video/ 
Run Code Online (Sandbox Code Playgroud)

但现在我需要能够将视频文件添加到上传中.我一直在寻找有关使用Tastypie上传文件的几个小时,我还没有想出一个坚实的回复.我需要添加Base64编码吗?如果是这样的话?在我通过POST请求上传文件后如何访问该文件?只是正常的request.FILES动作?我不是要将文件保存到数据库,只是获取文件的路径.

#Models.py
class Video(models.Model):
    video_uploader = models.ForeignKey(User)
    video_path = models.CharField(max_length=128)
    video_views = models.IntegerField(default=0)
    upload_date = models.DateTimeField(auto_now_add=True)
    video_description = models.CharField(max_length=860)
    video_title = models.SlugField()
Run Code Online (Sandbox Code Playgroud)

我对如何为Tastypie实现文件上传系统感到非常困惑,所以任何帮助都将非常感激.谢谢!

python django tastypie

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

Django inheritance base class super

I would like to use inheritance and have all my resources inheritance from a base resource class.

You will see what I have tried so far below. My issues is I now need to add in the meta class at it seems to overwrite at the moment. How can this be done?

class BasedModelResource(ModelResource):
    class Meta:
        authentication = ApiKeyAuthentication()
        authorization = UserObjectsOnlyAuthorization()



class AccountResource(BasedModelResource):
    """
    Account Object Resource
    """
    class Meta:
        queryset = Account.objects.all()
        resource_name = 'account'
Run Code Online (Sandbox Code Playgroud)

django tastypie

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

Django Tastypie和Django 1.4

如果我想将django-tastypie与我已经在Django 1.4中启动的项目集成,它会起作用吗?如果只是部分,有什么影响?如果不是,那么在我开始尝试集成它之前,这将是一件好事.我也不希望在Django 1.5中重建整个应用程序,理想情况下可以将Tastypie添加到我现有的1.4项目中.

谢谢你的建议!

api django integration tastypie django-1.4

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

如何处理tastypie中的循环进口

我正在开发一个django-tastypie网络应用程序.我有两个django型号:

class Student(models.Model):
  name = models.CharField()

class Course(models.Model):
  name = models.CharField()
  student = models.ForeignKey(Student)
Run Code Online (Sandbox Code Playgroud)

从那以后,我在两个不同的文件中有两个Tastypie资源.但这是我的问题.我希望能够从课程中过滤学生,从学生那里过滤课程:

from website.api.course import CourseResource

class StudentResource(ModelResource):
  course = fields.ForeignKey(CourseResource, "course")

  class Meta:
    queryset = Student.objects.all()
    resource_name = "student"
    filtering = { "course" : ALL }
Run Code Online (Sandbox Code Playgroud)

from website.api.student import StudentResource

class CourseResource(ModelResource):
  student = fields.ForeignKey(StudentResource, "student")

  class Meta:
    queryset = Course.objects.all()
    resource_name = "course"
    filtering = { "student" : ALL }
Run Code Online (Sandbox Code Playgroud)

但当然,我遇到了循环导入问题.我怎么能解决这个问题?

谢谢 !

python django circular-dependency tastypie

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

Python-使用httplib放入JSON数据

我正在尝试在tastypie-Django Python应用程序中使用JSON数据执行简单的PUT。但是我通过代码调用时看到401响应错误,但是从终端执行cURL命令时却没有错误。我有代码:

data_to_update = json.dumps({ "NAME" : username,
                              "type": mytype
                            })

headers = { "Content-type": "application/json",
            "Authorization: ApiKey": '{0}:{1}'.format(username, key)
          }

conn = httplib.HTTPConnection('localhost:8000')
conn.set_debuglevel(1)

conn.request('PUT', '/api/v1/table/1/', data_to_update, headers=headers)

response = conn.getresponse()
print response.status, response.reason
conn.close()
Run Code Online (Sandbox Code Playgroud)

我看到输出:

send: u'PUT /api/v1/table/10/ HTTP/1.1\r\nHost: localhost:8000\r\nAccept-Encoding: identity\r\nContent-Length: 148\r\nContent-type: application/json\r\nAuthorization: ApiKey: api:79910a14-a82c-41f9-bb79-458247e6b31a\r\n\r\n{"username": "johnny", "type": "admin_user", "resource_uri": "/api/v1/table/10/"}'
reply: 'HTTP/1.0 401 UNAUTHORIZED\r\n'
header: Date: Fri, 15 Aug 2014 20:07:36 GMT
header: Server: WSGIServer/0.1 Python/2.7.5
header: X-Frame-Options: SAMEORIGIN
header: Content-Type: text/html; charset=utf-8
401 UNAUTHORIZED
Run Code Online (Sandbox Code Playgroud)

但是当我cURL通过终端运行命令时: …

python httplib tastypie

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