我正在为Django网站创建一个RESTful api.我正在使用tastypie来做这件事.我的问题是我无法设计如何通过此api发布图像或文件.我的意思是,要在数据库上创建一个对象,我们将以json格式发布数据.但是我如何将文件放在json中呢?
我发现有两种方法,其中一种是将它们转换为Base64格式.我不想使用它,因为在我的测试中,当转换为Base64时,74kb的图像是110kb-120kb.
所以有人可以解释我如何发布一个包含文件的对象?
我有GFK的Page模型.
class Page(models.Model):
title = models.CharField(max_length=200)
content_type = models.ForeignKey(ContentType,null=True,blank=True)
object_id = models.CharField(max_length=255,null=True,blank=True)
content_object = generic.GenericForeignKey('content_type', 'object_id')
Run Code Online (Sandbox Code Playgroud)
和
class TextContent(models.Model):
content = models.TextField(null=True, blank=True)
pages = generic.GenericRelation(Page)
Run Code Online (Sandbox Code Playgroud)
我做了Page.objects.get(pk = 1).content_object,我明白了.
请帮我看一下REST中锚定对象的链接(或输出到JSON).
class PageResource(ModelResource):
content_object = fields.?????
class Meta:
queryset = Page.objects.all()
resource_name = 'page'
Run Code Online (Sandbox Code Playgroud)
怎么做对了?
谢谢!
维塔利
class LinguistResource(ModelResource):
class Meta:
model = Linguist
queryset = Linguist.objects.all()
resource_name = 'linguists_by_language'
filtering = {
"language": ('exact', ),
}
Run Code Online (Sandbox Code Playgroud)
是否可以强制使用"语言"过滤器?
如果在GET参数中没有关键的"语言",我的目标是引发错误
我希望特定django-tastypie
模型资源在列出对象时只有一个字段子集,在显示细节时只有所有字段.这可能吗?
我在申请中看到了一些常见的事情.当我很少或没有流量时,我的服务器无缘无故地放慢速度.经过大量的试验和错误后,我发现当我删除了ToOneField
我的TastyPie资源时,我的问题就消失了!
我发现的是由于一些未知的原因,TastyPie正在这些ToOneFields上进行数据库更新,这是没有充分理由的!什么......时刻!
我在这里发现了一个可能的错误,声称修复了更新问题.我已经安装了最新版本,pip
但仍然看到了这个问题.
有人可以帮忙吗?
class IncentiveResource(ModelResource):
product_introducer = fields.ToOneField(ProductResource, 'referrer_product', full=True)
product_friend = fields.ToOneField(ProductResource, 'referee_product', full=True)
class Meta:
queryset = Incentive.objects.all().order_by('-date_created')
resource_name = 'incentive'
allowed_methods = ['get']
authentication = MultiAuthentication(ClientAuthentication(), ApiKeyAuthentication())
authorization = Authorization()
filtering = {
"active": ALL,
}
always_return_data = True
cache = SimpleCache(cache_name='resources', timeout=10)
Run Code Online (Sandbox Code Playgroud)
这里交通很少,但变得无法使用.
我想返回一些JSON响应,而不是只返回带有错误代码的标头.在tastypie中是否有办法处理这样的错误?
我正在使用这里描述的类似外观的模式:http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html
def obj_get(self, request=None, **kwargs):
rv = MyObject(init=kwargs['pk'])
audit_trail.message( ... )
return rv
Run Code Online (Sandbox Code Playgroud)
我不能返回None,抛出一个错误.
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 …
我有一个正在研究的tastypie api,在我的api资源的列表视图中,无论列表中的对象数量多少,我都希望得到整个数据列表而不应用分页.我不需要具有高限制的自定义分页符,我想完全禁用分页.
我可能会修改我的客户端以处理分页(api是从C++ DLL而不是Web浏览器访问所以它有点复杂但可能)但是如果我可以禁用它会更容易.
是否有一个开关来禁用不同资源的paginator,或者可能是一个api wide开关来禁用注册到该api对象的所有资源的分页?
我在Django中编写了一个机器学习应用程序,因此用户可以在表单中指定一些参数并训练模型.一旦模型被训练,我想要提供如下请求:
curl http://localhost:8000/.../?model_input='XYZ'
Run Code Online (Sandbox Code Playgroud)
我希望Django在给定输入XYZ的情况下返回模型的输出.我从Tastypie或REST框架中看到的每个示例都是从查询集构建其响应.如果响应不是查询集的结果而是内存中纯计算的结果,我该如何继续?在我的例子中,响应是由向量(输入)进行矩阵乘法(训练模型)的结果,并且该结果不存储在表中.
管理此类请求的推荐方法是什么?任何帮助是极大的赞赏.此致,帕特里克