我有一个非常简单的嵌套模型。
class Game(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
user = models.ForeignKey(User)
class Activity(models.Model):
game = models.ForeignKey(Game, related_name = 'activities')
type = models.CharField(max_length=4)
score = models.DecimalField(max_digits=3, decimal_places=2)
我可以通过管理员创建对象,并且它可以顺利运行。然后我用TastyPie创建了一个API。我的资源看起来像这样:
class GameResource(ModelResource):
user = fields.ToOneField(UserResource, 'user')
app = fields.ToOneField(AppResource, 'app')
activities = fields.ToManyField('api.resources.ActivityResource', 'activities', full=True, related_name='activity')
class Meta:
queryset = Game.objects.all()
resource_name = 'games'
list_allowed_methods = ['get','post']
detail_allowed_methods = ['get','put','post','delete']
authentication = ApiKeyTokenAuthentication()
authorization = Authorization()
serializer = CustomJSONSerializer(formats=['json'])
def apply_authorization_limits(self, request, object_list):
return object_list.filter(user=request.user)
def obj_create(self, bundle, request=None, **kwargs):
requestApp = App.objects.get(api_key=request.api_key)
return super(GameResource, self).obj_create(bundle, request, app=requestApp)
def get_object_list(self, request):
return super(GameResource, self).get_object_list(request).filter(app__api_key=request.api_key)
class ActivityResource(ModelResource):
game = fields.ForeignKey(GameResource, 'game')
class Meta:
queryset = Activity.objects.all()
resource_name = 'activities'
list_allowed_methods = ['get','post']
detail_allowed_methods = ['get','put','post','delete']
authentication = ApiKeyTokenAuthentication()
authorization = Authorization()
serializer = CustomJSONSerializer(formats=['json'])
def apply_authorization_limits(self, request, object_list):
return object_list.filter(game__user=request.user)
def obj_create(self, bundle, request=None, **kwargs):
return super(ActivityResource, self).obj_create(bundle, request)
def get_object_list(self, request):
return super(ActivityResource, self).get_object_list(request).filter(game__app__api_key=request.api_key)
我在应用程序中发布了这个字典,其中的活动嵌套在游戏中。
{
name = "Monte";
description = "To search for the holy grail.";
user = "/api/v1/users/2/";
activities = (
{
type = "eggs";
score = "0.50";
}
);
}
结果是Game在数据库中创建,但是当TastyPie尝试创建嵌套的Activity对象时,它会发出错误。错误说当它试图获取活动列表时,它无法找到与api_key匹配的那些。我不明白为什么它试图获取对象,我不明白NoneType来自哪里,如果它正在使用发送请求的应用程序正确创建游戏。您可以看到我找到了与request.api_key匹配的应用,并将其设置为游戏的应用。
"error_message": "'NoneType' object has no attribute 'api_key'"
super(ActivityResource, self).get_object_list(request).filter(game__app__api_key=request.api_key)\n\nAttributeError: 'NoneType' object has no attribute 'api_key'\n"
我错过了什么?我的资源和我获得Actvities的方式有问题吗?
作为旁注......我可以发一本字典:
{
game = "/api/v1/games/2/"
type = "eggs";
score = "0.50";
}
当我将其发布到活动时,它会正确创建活动。当我通过API获得活动时,它看起来就像那样。
所以问题不在于创建活动,而只是在游戏中嵌套创建它们。
更新
我添加了一些断点,发现在resource_from_data中它调用了obj_update而没有发送请求,因此request = NoneType。这是TastyPie中的错误吗?
def resource_from_data(self, fk_resource, data, request=None, related_obj=None, related_name=None):
"""
Given a dictionary-like structure is provided, a fresh related
resource is created using that data.
"""
# Try to hydrate the data provided.
data = dict_strip_unicode_keys(data)
fk_bundle = fk_resource.build_bundle(data=data, request=request)
if related_obj:
fk_bundle.related_obj = related_obj
fk_bundle.related_name = related_name
# We need to check to see if updates are allowed on the FK
# resource. If not, we'll just return a populated bundle instead
# of mistakenly updating something that should be read-only.
if not fk_resource.can_update():
return fk_resource.full_hydrate(fk_bundle)
try:
return fk_resource.obj_update(fk_bundle, request, **data)
except NotFound:
try:
# Attempt lookup by primary key
lookup_kwargs = dict((k, v) for k, v in data.iteritems() if getattr(fk_resource, k).unique)
if not lookup_kwargs:
raise NotFound()
return fk_resource.obj_update(fk_bundle, **lookup_kwargs)
except NotFound:
return fk_resource.full_hydrate(fk_bundle)
except MultipleObjectsReturned:
return fk_resource.full_hydrate(fk_bundle)