我在使用带有非常普通的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"我想要的是获得一个促销列表,像www.server.com/api/v1/promos?format=json&category__description=XXX这样的描述进行过滤
首先,请注意CatPromosResource的元类中的"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调用的正确方法,请帮助我。
发布于 2013-04-03 16:48:03
尝试使用:
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/
https://stackoverflow.com/questions/13006281
复制相似问题