首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tastypie获取manyTomany错误

Tastypie获取manyTomany错误
EN

Stack Overflow用户
提问于 2012-10-22 14:53:54
回答 1查看 345关注 0票数 1

我在使用带有非常普通的m2m关系的Tastypie时遇到了一个问题。在我的(简化)模型中:

代码语言:javascript
复制
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')

在我的资源中:

代码语言:javascript
复制
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"

我想要的是获得一个促销列表,像www.server.com/api/v1/promos?format=json&category__description=XXX这样的描述进行过滤

首先,请注意CatPromosResource的元类中的"detail_uri_name“。由于detail_uri_name的一些问题,Tastypie (最新版本)一直崩溃。默认为pk,但使用它的对象需要_pk_val。这一点,我在经过一些调试后意识到了。但现在的问题是,每当我使用上面的uri调用GET时,服务器都会崩溃,并显示以下消息:

代码语言:javascript
复制
"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调用的正确方法,请帮助我。

EN

回答 1

Stack Overflow用户

发布于 2013-04-03 16:48:03

尝试使用:

代码语言:javascript
复制
category =  fields.ToManyField(CatPromosResource, 'category', null=True)

而是:

代码语言:javascript
复制
category =  fields.ForeignKey(CatPromosResource, 'category', full=True, null=True)

另外,这也可以帮助你:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13006281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档