2012-09-09 27 views
0

我是比較新的Django和Django的tastypie,所以我的問題可能是相當瑣碎。Django的Tastypie - 本地化,DB模式

我試圖實現與tastypie爲本地化的文本RESTful服務。

我的Django的資源有:

#The different languages 
class Language(models.Model): 
    abbreviation = models.CharField(max_length=6) 
    name = models.CharField(max_length=100) 

# The different Chapters in the book 
class Chapter(models.Model): 
    name = models.CharField(max_length=200) 

# The different Lines of the chapters (by sections) 
class Line(models.Model): 
    chapter= models.ForeignKey(Chapter) 
    section = models.IntegerField() 
    line = models.IntegerField() 

# The different translations of the line 
class LineLanguage(models.Model): 
    language = models.ForeignKey(Language) 
    line = models.ForeignKey(Line) 
    text = models.TextField() 

我那麼有tastypie資源:

class LanguageResource(ModelResource): 
    class Meta: queryset = Language.objects.all() 
    resource_name = 'language' 
    authorization = Authorization 

class ChapterResource(ModelResource): 
    class Meta: queryset = Chapter.objects.all() 
    resource_name = 'chapter' 
    authorization = Authorization() 

class LineResource(ModelResource): 
    chapter = fields.ForeignKey(ChapterResource, 'chapter') 
    class Meta: queryset = Line.objects.all() 
    resource_name = 'line' 
    authorization = Authorization() 

class LineLanguageResource(ModelResource): 
    language= fields.ForeignKey(LanguageResource, 'language') 
    line = fields.ForeignKey(LineResource, 'line') 
    class Meta: queryset = LineLanguage.objects.all() 
    resource_name = 'lineLanguage' 
    authorization = Authorization() 

起初我只想使用RESTful服務(因此我url.py僅包含 「url(r'^api/', include(v1_api.urls)),」 ,但我無法得到我想要的:

我希望能夠通過一個單一的寧靜調用來檢索文本的特定行正確translat離子,但是在不知道LineLanguageID的情況下它是不可能的。

在使用tastypie之前。我曾在我的url.py,讓我用這種www.myapp.com/api/v1/language/chapter/section/line的網址找到它的一些設置(以便於如www.myapp.com/api/v1/en/1/1/1會回到我的英文第一章第一部分的第一行),但我想切換到RESTFUL api(主要是因爲我想試驗backbone.js)

我相信我的問題或者是我的模型/ tastypie資源的糟糕設計,或者缺乏關於如何將我的模型轉換爲適當的tastypie的知識資源。 你有什麼建議?

回答

1

你或許可以設計你的模型略有不同。我不知道你的另一需求是什麼,但你或許可以改變模型是這樣的:

#The different languages 
class Language(models.Model): 
    abbreviation = models.CharField(max_length=6) 
    name = models.CharField(max_length=100) 

# The different Lines of the chapters (by sections) 
class Line(models.Model): 
    name = models.CharField(max_length=200) 
    section = models.IntegerField() 
    line = models.IntegerField() 
    language = models.ForeignKey(Language) 
    text = models.TextField() 

你將不得不定義只有一個資源爲Line模型,你會查詢語言/節用類似的請求/章節/線:

www.myapp.com/api/v1/line/?language__abbreviation=en&section=1&chapter=1&line=1 

如果你不想通過調用API的東西,如改變你的模型,你可以達到你想要的東西:

www.myapp.com/api/v1/lineLanguage/?language__abbreviation=en&line__line=1&line__section=1&line__chapter__name=1 

請注意,對於這兩種情況,資源的外鍵字段都需要設置full=True參數,因爲您需要查詢外國模型的字段。

如果您想要使用舊的url模式,您可以通過在LineLanguageResource中定義override_urls方法來實現該方法,該方法將包含將處理來自url的參數的模式,將它傳遞給某種方法,然後再通過它們以元素'dispatch_detail方法作爲request.GET元素。

+0

我曾考慮過改變模式(這是一種可能性),但我對這種數據庫設計的表現有點擔心。我想我會嘗試保留我當前的數據庫模式,也許評估如果使用tastypie是我的問題的最佳解決方案。謝謝! – srodriguez