6

私はジャンゴのおいしいパイを始めたばかりで、熱心に取り組んでいます。私の質問: 管理ビューと同じ機能を探しています: 外部キー フィールドに、他のオブジェクトのリスト応答で何を表示し、詳細応答で何を表示するかを指定します。

これが私の単純化されたモデルだとしましょう:

class Location(models.Model):
    name = models.CharField(max_length=256, blank=True)
    longitude = models.FloatField(blank=True, default=0.0)
    latitude = models.FloatField(blank=True, default=0.0)
    description = models.CharField(max_length=256, blank=True)
    shortname = models.CharField(max_length=256, blank=True)
    tooltiptext = models.CharField(max_length=1000, blank=True)
    locationtype = models.ForeignKey(LocationType, blank=True, null=True)
    public_anonymous = models.BooleanField(default=False, blank=False, null=False)
    public_authorized = models.BooleanField(default=False, blank=False, null=False)
    def __str__(self):
        return '%s' % (self.name)

class Variable(models.Model):
    abbreviation = models.CharField(max_length=64, unique=True)    
    name = models.CharField(max_length=256, blank=True)
    unit = models.CharField(max_length=64, blank=True)
    def __str__(self):
        return '%s  [%s]' % (self.name, self.unit)

class Timeseries(models.Model):
    locationkey = models.ForeignKey(Location)
    variablekey = models.ForeignKey(Variable)
    tstypekey = models.ForeignKey(TimeseriesType)

    def __str__(self):
        return '%s: %s (%s)' % (self.locationkey.name, self.variablekey.name, self.tstypekey.name)

これらはAPIの私のリソースです:

class LocationResource(ModelResource):
    class Meta:
        queryset = Location.objects.all()
        resource_name = 'location'
        excludes = ['public_anonymous', 'public_authorized']
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

class VariableResource(ModelResource):
    class Meta:
        queryset = Variable.objects.all()
        resource_name = 'variable'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

class TimeseriesTypeResource(ModelResource):
    class Meta:
        queryset = TimeseriesType.objects.all()
        resource_name = 'timeseriestype'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

class TimeseriesResource(ModelResource):
    location = fields.ForeignKey(LocationResource, 'locationkey', full=False) 
    variable = fields.ForeignKey(VariableResource, 'variablekey', full=False) 
    timeseriestype = fields.ForeignKey(TimeseriesTypeResource, 'tstypekey', full=False) 

    class Meta:
        queryset = Timeseries.objects.all()
        resource_name = 'timeseries'
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()

を使用する場合は TimeseriesResourcefull=Falseで、ID を含む URL を取得するだけで、使用full=Trueするとすべての情報を取得できます。実際には、場所にはもっと多くの情報があります。full='False' よりも少し多くの情報が必要ですが、を使用しているすべての情報ではありませんfull=True。詳細情報または Location オブジェクト自体のリストに情報がないため、除外オプションを使用したくありません。

私が考えていたオプションの 1 つは、同じオブジェクトに対して 2 つのリソースを作成することですが、これは最善の解決策とは思えません (しかし、うまくいくと思います)。ところで:私はこのオプションを考えましたが、うまくいきません(もちろん)、bmihelacの回答にある回避策を使用することをお勧めします(ありがとう)。

ただし...回避策を試すと...新しい質問につながります。次を参照してください。

django-tastypie: dehydrate(self,bundle) で bundle.request にアクセスできません

4

3 に答える 3

5

と のさまざまなフィールドに対する機能要求がありshowindexそれを実装する方法についていくつかの議論があります。

https://github.com/toastdriven/django-tastypie/issues/18

この機能が含まれなくなるまで、次の回避策を手伝ってください:

https://github.com/toastdriven/django-tastypie/issues/18#issuecomment-2695447

于 2011-11-23T17:53:12.183 に答える