2011-11-23 39 views
6

我剛開始使用django tastypie,並且我對它很感興趣。 我的問題:我正在尋找與管理視圖中相同的功能: 爲外鍵字段指定了在其他對象的列表響應中看到的內容以及詳細響應中的內容。Django tastypie:詳細請求中的資源顯示與列表請求中的不同

讓我們說這是我的simplyfied型號:

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() 

在TimeseriesResource如果你使用full=False,你只會得到一個URL以編號,如果您使用full=True您將獲得所有信息。實際上,這個位置有很多信息。 我只需要比full ='False'更多的信息,但不是所有使用full=True的信息。 我不想使用排除選項,因爲我沒有詳細信息或位置對象列表中的信息itselve。

我正在考慮的選項之一是爲相同的對象製作2個資源,但這不像是最好的解決方案(但我猜它會起作用)。順便說一下:我認爲這個選項不會起作用(更好),更好地使用bmihelac的答案中所使用的解決方法(謝謝)。

雖然... 試圖解決辦法... 我引到一個新的問題,請參見:

django-tastypie: Cannot access bundle.request in dehydrate(self,bundle)

回答

5

有在showindex不同領域的功能要求,與一些alnong討論如何可以實現:

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

直到此功能爲n OT包括,也許你可以用這個解決方法幫助:

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

+1

包括答案中的片段,我會投它:) – ashwoods

+0

謝謝,至少我知道不要再搜索!解決方法看起來不錯(不完美,但它應該做的伎倆) –

+0

看到我的答案一個愉快的更新! – mlissner

0

順便說一句,我切換到另一個Django的REST類型的API框架看http://django-rest-framework.org/。所以我不再使用tastypie了。 Hoewever,這個問題和答案可能對其他人有用。

我發現django-rest-framework在實現它的方式上更容易,更靈活。我沒有發現任何我無法用這個框架做的事情(好吧,它不會給我帶來咖啡)。

5

好消息!這已添加到tastypie。

如果您希望將字段設置爲僅在特定頁面上,只需將use_in屬性定義爲'all','list'或'detail'即可。

更多信息here,但我會爲後人提供了一個例子:

class DocumentResource(ModelResource): 
    my_field = fields.CharField(attribute='my_field', use_in='detail') 

就這麼簡單!