我正在尝试使用Tastypie工作创建新实例,但我不断使用外键获得此错误.这是我的东西:
楷模:
class SuggestionVote(models.Model):
created_by_user = models.ForeignKey(User)
date_created = models.DateTimeField(auto_now_add = True)
suggestion = models.ForeignKey(Suggestion)
class Suggestion(models.Model):
title = models.TextField(blank=True,null=True)
created_by_user = models.ForeignKey(User)
date_created = models.DateTimeField(auto_now_add = True)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.title
Run Code Online (Sandbox Code Playgroud)
模型资源(我使用自己的身份验证方法):
class UserResource(ModelResource):
class Meta:
list_allowed_methods = ['get']
queryset = User.objects.all()
resource_name = 'user'
authentication = MyBasicAuthentication()
authorization = DjangoAuthorization()
class SuggestionResource(ModelResource):
class Meta:
list_allowed_methods = ['get']
queryset = Suggestion.objects.all()
resource_name = 'suggestion'
authentication = MyBasicAuthentication()
authorization = DjangoAuthorization()
class SuggestionVoteResource(ModelResource):
class Meta:
list_allowed_methods = …Run Code Online (Sandbox Code Playgroud) 我有一个Tastypie ModelResource,它从常规Django模型中获取其字段.我想在Tastypie资源上使某些字段只读,即使它们在底层模型中是可写的.这有可能以简单的方式完成吗?
我试过以下无济于事:
def __init__(self, **kwargs):
super(ModelResource, self).__init__(**kwargs)
for f in getattr(self.Meta, 'read_onlys', []):
self.fields[f].read_only = True
Run Code Online (Sandbox Code Playgroud) 我想要做的是每当用户请求API密钥时 - 无论用户是否已生成一个 - 系统将生成一个全新的密钥.
我知道每当调用时ApiKey.objects.create()都会为没有生成的用户生成一个API密钥.但是,如果用户确实有一个,则尝试调用该.create()方法会引发错误.
在这种情况下,我认为最好编写自己的密钥生成器.但是,我现在希望这里的某个人可能知道一个帮助函数,它允许我生成一个随机的API密钥,然后让我自己手动将它保存到数据库中.
有人可能知道任何这样的帮手功能吗?
好的所以我正在设置一个API.一切正常.我正在通过OAuth2 python lib创建一个令牌.我正在使用TastyPie作为我的API.
我面临的问题是,AccessToken或Client模型中没有"创建"令牌方法.
我可以通过Django管理员创建一个accessToken,我可以通过卷曲来创建一个:
myhost.com/oauth2/access_token(包含所有信息,密钥,客户端ID,用户和密码)
我的目标是成功注册用户的API,oAuth客户端自动创建(工作)但我也想生成AccessToken.我不能卷曲自己的服务器作为它给了我一个重定向/拒绝连接错误,所以我想以编程方式做到这一点在Python.无论如何要做到这一点?这是一个片段:
try:
user = User.objects.create_user(username, password)
user.save()
if user:
oauth_client = Client(user=user, name="api account", client_type=1, url="http://example.com")
oauth_client.save()
oauth_client_id = oauth_client.pk
oauth_client_secret = oauth_client.client_secret
if oauth_client:
print user
print oauth_client_id
print AccessToken.objects.all()
print '........'
token = AccessToken(user=user, client=oauth_client_id, scope=6)
token.save()
Run Code Online (Sandbox Code Playgroud)
上面的最后两行,虽然没有错误..将不会保存新的AccessToken.有任何想法吗?谢谢StackOverflow !!!!
我们正在使用Django/TastyPie作为后端REST服务提供程序构建Web应用程序,并构建基于AngularJS的前端,使用大量基于$ resource的服务来处理服务器上的CRUD对象.到目前为止,一切都很好!
但是,当我们想要仅更新对象上的一个或两个已更改字段时,我们希望减少我们传送的数据量.
TastyPie使用HTTP PATCH方法支持此功能.我们在对象上定义了一个.diff()方法,因此我们可以确定在进行更新时要发送哪些字段.我找不到任何关于如何在$ resource返回的实例对象上定义/实现该方法以执行我们想要的文档.
我们想要做的是向对象实例添加另一个方法(如此处的Angular.js文档中所述),如myobject.$ partialupdate(),它将:
到目前为止,我找不到任何描述如何执行此操作的文档(或其他SO帖子),但非常感谢任何人可能提出的任何建议.
谢谢.
在resources.py我有:
class CategoryResource(ModelResource):
items = fields.ToManyField('ItemResource', 'items', full=True, null=False, readonly=True, related_name='items')
class Meta:
queryset = Category.objects.all().order_by('id')
include_resource_uri = False
always_return_data = True
resource_name = 'category'
Run Code Online (Sandbox Code Playgroud)
6类中约有5000种.当我制作'list'api请求即'api/1.0/category'时,它会对数据库进行大约5000次查询.我该如何优化它?我知道full = False,但它不适合我的需要.
UPD: 我发现了导致如此多查询的原因.我在ItemResource中有'类别'关系,因此tastypie为每个项生成一个选择查询.
class ItemResource(ModelResource):
categories = fields.ToManyField(CategoryResource, 'categories', null=True, readonly=True)
def dehydrate_categories(self, bundle):
categories = Category.objects.filter(owner_id=bundle.request.user.id, items__item=bundle.obj)
return [category.name for category in categories]
Run Code Online (Sandbox Code Playgroud)
当我请求CategoryResource时,显然这是不必要的数据,有没有办法将它从查询中排除?
我正在为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)
怎么做对了?
谢谢!
维塔利
我希望特定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)
这里交通很少,但变得无法使用.
