Tastypie返回字符串表示而不是有效数组以进行反向外键查找

时间:2012-11-30 06:55:57

标签: django rest tastypie

更新:LocationResource中的脱水是否应该责备?

我有一种常见的情况,即通过foreignkey的反向查找返回数组的嵌套字符串表示,而不是数组本身。 [I discovered this while using tastypie with backbone.js and backbone.marionette.][1]

奇怪的是,即使从fields.ForeignKey资源返回嵌套数组

,在执行“转发”或正常查找时也不会发生这种情况。

例如:

正向查询:

[{"adult_price": "123", "child_price": "123", "currency": [{"abbrev": "USD", ... }] } ]
//--> notice that "currency" simply returns an array, without " "

反向查找 - 通过外键“向后”

[{"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

以下是资源的设置,然后是模型。

TASTYPIE资源

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 个答案:

答案 0 :(得分:0)

根据您的型号:

class Tours(models.Model):

    location = models.ForeignKey('app.Location', related_name="theTours")
    ....
    ....

class Location(models.Model):

    ....

您希望使用反向关系,例如:Location.theTours.all(),因此您需要将其添加到LocationResource

theTours = fields.ToManyField(ToursResource, 'theTours', full = True)

第一个theTours可以是您想要的任何内容,第二个应该与related_name相同。

不需要dehydrate

最终结果:

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"),
        ]