2

更新:LocationResource 中的脱水是否应归咎于?

我有一种常见的情况,即通过外键进行反向查找返回的是数组的嵌套字符串表示,而不是数组本身。[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

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

美味资源

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

DJ 模型

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
4

1 回答 1

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"),
        ]  
于 2012-11-30T07:36:03.727 回答