Tastypie返回字符串表示而不是用于反向外键查找的有效数组 [英] Tastypie returns string representation rather than valid array for reverse foreignkey lookup
问题描述
更新:LocationResource中的脱水是否怪?
我有一个通常的情况,通过外键的反向查找返回数组的嵌套字符串表示,而不是数组本身。 [我发现这一点,而使用taspypie与backbone.js和骨干。marionette。] [1]
奇怪的是,即使从fields返回嵌套数组,执行转发或正常查找也不会发生.ForeignKey资源
例如:
正向查找
[{adult_price:123,child_price:123,货币:[{缩写:USD,...}]} b $ b // - >注意货币只是返回一个数组,而没有
反向查询 - 向后通过foreignkey
[{id:1,name:威尼斯,resource_uri:/ api / v1 / location / 1 /,
theTours:[{'subtitle:... ...}]}]
// - >请注意,在theTours之外有一组引号。
// - >数组的字符串表示
以下是资源的设置,然后是模型。 / p>
TASTYPIE RESOURCES
class LocationResource ModelResource):
class Meta:
queryset = Location.objects.all()
resource_name ='location'
def脱水(self,bundle ):
bundle.data ['theTours'] = Tours.objects.filter(location__name = bundle.obj.name).values()
return bundle
filters = {
'name':ALL_WITH_RELATIONS,
}
def override_urls(self):
return [
url(r^(?i)(?P< resource_name& %s)/(?P< name> [\w\d _.-] +)/ $%self._meta.resource_name,self.wrap_view('dispatch_detail'),name =api_dispatch_detail),
]
class CurrencyResource(ModelResource):
class Meta:
queryset = Currency.objects.all()
resource_name ='currency'
filters = {
'name':ALL,
'abbrev':ALL,
class ToursResource(ModelResource):
day = fields.ToManyField(DayOfWeekResource,'day',full = True)
location = fields.ForeignKey LocationResource,'location',full = True,related_name =theLocs)
currency = fields.ToManyField(CurrencyResource,'currency',full = True)
class Meta:
queryset = Tours.objects.all()
resource_name ='tours'
limit = 100
filtering = {
'day':ALL_WITH_RELATIONS,
'location' :ALL_WITH_RELATIONS,
'currency':ALL_WITH_RELATIONS,
}
def override_urls(self):
return [
url(r^(?i ?)(P< RESOURCE_NAME>%S) //??%self._meta.resource_name,self.wrap_view('dispatch_detail'),name =api_dispatch_detail),
url(r^(?i)(?P< resource_name>%s)/ location /(?P< location__name> [\w\d _.-] +)/ $%self._meta.resource_name, self.wrap_view('dispatch_list'),name =api_dispatch_list),
]
DJANGO MODELS
class Tours(models.Model):
location = models.ForeignKey('app.Location',related_name =theTours)
name = models.CharField(max_length = 500)
currency = models.ManyToManyField('app.Currency')
class Meta:
verbose_name_plural =Tours
def __unicode __(self):
return self.name
class位置(models.Model):
name = models.CharField(max_length = 50,unique = True)
def __unicode __(self) b $ b return self.name
class货币(models.Model):
abbrev = models.CharField(max_length = 5,unique = True)
name = models.CharField(max_length = 50,unique = True)
symbol = models.CharField(max_length = 5)
class Meta:
verbose_name_plural =货币
def __unicode __(self):
return self.name
根据您的模型:
class Tours(models.Model):
location = models.ForeignKey('app.Location',related_name =theTours)
....
....
class位置(模型.Model):
....
你想使用反向关系如下: Location.theTours.all()
,所以你需要添加到 LocationResource
:
theTours = fields.ToManyField(ToursResource,'TheTours',full = True)
第一个 theTours
可以是任何你想要的,第二个应该是相同的 related_name
。
不需要脱水
。
最终结果:
class LocationResource(ModelResource):
theTours = fields.ToManyField(ToursResource,'theTours',full = True)
class Meta:
queryset = Location.objects.all()
resource_name ='location'
def override_urls(self):
return [
url(r^(?i)(?P< resource_name>%s)/(?P< name> \w\d _.-] +)/ $%self._meta.resource_name,self.wrap_view('dispatch_detail'),name =api_dispatch_detail),
]
UPDATE: Is the dehydrate within the LocationResource to blame?
I have an usual situation where a reverse lookup via a foreignkey is returning a nested string representation of an array, rather than the array itself. [I discovered this while using tastypie with backbone.js and backbone.marionette.][1]
The strange thing is that this does NOT happen when doing a "forward" or normal lookup, even if there are nested arrays returned from fields.ForeignKey resources
For example:
Forward lookups:
[{"adult_price": "123", "child_price": "123", "currency": [{"abbrev": "USD", ... }] } ]
//--> notice that "currency" simply returns an array, without " "
Reverse lookups - "backwards" via a foreignkey
[{"id": "1", "name": "Venice", "resource_uri": "/api/v1/location/1/",
"theTours": "[{'subtitle': ... ... }]" } ]
//--> notice that there is a set of quotes " " outside the array for "theTours".
//--> a string representation of the array
The following is the setup of the resources, and then the models.
TASTYPIE RESOURCES
class LocationResource(ModelResource):
class Meta:
queryset = Location.objects.all()
resource_name = 'location'
def dehydrate(self, bundle):
bundle.data['theTours'] = Tours.objects.filter(location__name=bundle.obj.name).values()
return bundle
filtering = {
'name' : ALL_WITH_RELATIONS,
}
def override_urls(self):
return [
url(r"^(?i)(?P<resource_name>%s)/(?P<name>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
class CurrencyResource(ModelResource):
class Meta:
queryset = Currency.objects.all()
resource_name = 'currency'
filtering = {
'name' : ALL,
'abbrev' : ALL,
}
class ToursResource(ModelResource):
day = fields.ToManyField(DayOfWeekResource, 'day', full=True)
location = fields.ForeignKey(LocationResource, 'location', full=True, related_name="theLocs")
currency = fields.ToManyField(CurrencyResource, 'currency', full=True)
class Meta:
queryset = Tours.objects.all()
resource_name = 'tours'
limit = 100
filtering = {
'day' : ALL_WITH_RELATIONS,
'location' : ALL_WITH_RELATIONS,
'currency' : ALL_WITH_RELATIONS,
}
def override_urls(self):
return [
url(r"^(?i)(?P<resource_name>%s)/day/(?P<day__day_of_week>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
url(r"^(?i)(?P<resource_name>%s)/location/(?P<location__name>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_list'), name="api_dispatch_list"),
]
DJANGO MODELS
class Tours(models.Model):
location = models.ForeignKey('app.Location', related_name="theTours")
name = models.CharField(max_length=500)
currency = models.ManyToManyField('app.Currency')
class Meta:
verbose_name_plural = "Tours"
def __unicode__(self):
return self.name
class Location(models.Model):
name = models.CharField(max_length=50, unique=True)
def __unicode__(self):
return self.name
class Currency(models.Model):
abbrev = models.CharField(max_length=5, unique=True)
name = models.CharField(max_length=50, unique=True)
symbol = models.CharField(max_length=5)
class Meta:
verbose_name_plural = "Currencies"
def __unicode__(self):
return self.name
Based on your model :
class Tours(models.Model):
location = models.ForeignKey('app.Location', related_name="theTours")
....
....
class Location(models.Model):
....
You want to use reverse relation like:Location.theTours.all()
, so you need to add this to LocationResource
:
theTours = fields.ToManyField(ToursResource, 'theTours', full = True)
The first theTours
can be anything you want, the second one should be the same to related_name
.
No need dehydrate
.
Final result :
class LocationResource(ModelResource):
theTours = fields.ToManyField(ToursResource, 'theTours', full = True)
class Meta:
queryset = Location.objects.all()
resource_name = 'location'
def override_urls(self):
return [
url(r"^(?i)(?P<resource_name>%s)/(?P<name>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
这篇关于Tastypie返回字符串表示而不是用于反向外键查找的有效数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!