问题
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
回答1:
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"),
]
来源:https://stackoverflow.com/questions/13640412/tastypie-returns-string-representation-rather-than-valid-array-for-reverse-forei