我在使用具有非常正常的 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 的 Meta 类中的“detail_uri_name”。Tastypie(最新版本)由于 detail_uri_name 的一些问题而不断崩溃。默认为“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 调用的正确方法,请帮助我。