Tastypie GET manyTomany错误

时间:2012-10-22 06:53:54

标签: python django tastypie

使用非常正常的m2m关系的Tastypie时遇到问题。 在我的(简化)模型中:

class Promos(models.Model):
    promo_id = UUIDField(auto=True, unique=True, primary_key=True, null = False)
    title = models.CharField(max_length=400, default='title', null = False)
    text = models.TextField(max_length = 10000, null = False, default='text')
    category = models.ManyToManyField(CatPromos, null=True)
    active = models.BooleanField(null = False, default=False)

class CatPromos(models.Model):
    description = models.CharField(max_length = 10, unique=True, default='NoCat')

在我的资源中:

class PromosResource(ModelResource):
    category =  fields.ForeignKey(CatPromosResource, 'category', full=True, null=True)
    class Meta:
        object_class = Promos
        queryset = Promos.objects.all()
        allowed_methods = ['get']
        include_resource_uri = True
        authentication = Authentication()
        authorization = Authorization()
        always_return_data = False
        filtering = {"category":ALL_WITH_RELATIONS}

    def get_object_list(self, request, *args, **kwargs):
        return Promos.objects.filter(active=True)   

class CatPromosResource(ModelResource):
    class Meta:
        object_class = CatPromos
        queryset = CatPromos.objects.all()
        allowed_methods = ['get']
        include_resource_uri = True
        authentication = Authentication()
        authorization = Authorization()
        always_return_data = False
        filtering = {"description":ALL}
        detail_uri_name = "_pk_val"

我想要的是获取Promos列表,按照www.server.com/api/v1/promos?format=json&category__description=XXX

等说明进行过滤

首先,请注意CatPromosResource的Meta类中的“detail_uri_name”。由于detail_uri_name存在一些问题,Tastypie(最新版本)一直崩溃。默认值为“pk”,但使用它的对象需要“_pk_val”。那个,我经过一些调试后才意识到。 但现在的问题是,无论何时我用服务器上面的uri调用GET都会收到此消息:

"invalid literal for int() with base 10: ''", 
"traceback": "Traceback (most recent call last): 
File \"...python2.7/site-packages/tastypie/resources.py\", line 202, in wrapper
    response = callback(request, *args, **kwargs) 
File \"...python2.7/site-packages/tastypie/resources.py\", line 441, in dispatch_list
    return self.dispatch('list', request, **kwargs)
File \"...python2.7/site-packages/tastypie/resources.py\", line 474, in dispatch
    response = method(request, **kwargs)
File \".../python2.7/site-packages/tastypie/resources.py\", line 1135, in get_list       to_be_serialized[self._meta.collection_name] = [self.full_dehydrate(bundle) for bundle in bundles]
File \".../python2.7/site-packages/tastypie/resources.py\", line 739, in full_dehydrate
    bundle.data[field_name] = field_object.dehydrate(bundle)\  
File \".../python2.7/site-packages/tastypie/fields.py\", line 653, in dehydrate   
    return self.dehydrate_related(fk_bundle, self.fk_resource) 
File \".../python2.7/site-packages/tastypie/fields.py\", line 520, in dehydrate_related
    return related_resource.full_dehydrate(bundle) 
File \".../python2.7/site-packages/tastypie/resources.py\", line 739, in full_dehydrate
    bundle.data[field_name] = field_object.dehydrate(bundle)  
File \".../python2.7/site-packages/tastypie/fields.py\", line 121, in dehydrate    
    return self.convert(current_object)  
File \".../lib/python2.7/site-packages/tastypie/fields.py\", line 220, in convert    
    return int(value)\n\nValueError: invalid literal for int() with base 10: ''\n"}

我迷失在这里。我不知道该怎么办。我通过代码跟踪了调用,但我不知道如何解决它。如果有人知道如何面对这个问题,或者正确的方式来实现与m2m相关的GET调用,请帮助我。

1 个答案:

答案 0 :(得分:1)

尝试使用:

category =  fields.ToManyField(CatPromosResource, 'category', null=True)

相反:

category =  fields.ForeignKey(CatPromosResource, 'category', full=True, null=True)

此外,这可以帮助您:

http://eugene-yeo.me/2012/12/4/django-tastypie-manytomany-through-part-2/