1
使用Tastypie時非常普通的m2m關係時出現問題。 在我(簡體)型號:Tastypie GET manyTomany error
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過濾/茂德?格式= JSON & category__description = XXX
首先,請注意 「detail_uri_name」 詮釋的元級CatPromosResource的。 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調用的正確方法,請幫助我。