2012-07-18 77 views
0

我的Django模型是這樣的:如何使用django-tastypie爲繼承另一個模型的模型創建ModelResource?

class Session(models.Model): 
    ... 

class Document(models.Model): 
    session = models.ForeignKey(Session) 
    date_created = models.DateTimeField(auto_now_add=True) 

    class Meta: 
     abstract = True 

class Invoice(Document): 
    number = models.PositiveIntegerField() 
    # and some other fields 

class SupplyRequest(Document): 
    # fields here 

這樣,每次InvoiceSupplyRequest實例鏈接到Session並有date_created屬性。好。因此,我爲SessionInvoice創建了ModelResource,想象Tastypie可以透過Document模型字段。但是不起作用:

class SessionResource(ModelResource): 

    class Meta: 
     queryset = Session.objects.all() 
     ... 

class InvoiceResource(ModelResource): 

    session = fields.ForeignKey(SessionResource, 'session') 

    class Meta: 
     queryset = Invoice.objects.all() 
     ... 

當我試圖序列的發票,我得到了以下錯誤消息:

NoReverseMatch: Reverse for 'api_dispatch_detail' with arguments '()' and keyword arguments '{'pk': 1, 'resource_name': 'session'}' not found. 

有什麼辦法對付使用Tastypie模型繼承?

我忘了提及Document模型是一個抽象類。

+0

請添加你的url conf(s) – 2012-07-18 14:23:45

回答

2

我想你一定忘記了設置URL SessionResource。

from tastypie.api import Api 

api = Api() 

api.register(SessionResource()) 

urlpatterns += patterns('', 
    (r'^api/', include(api.urls)), 
) 

你在urls.py中這樣做嗎?

擁抱。

+0

上帝!疲勞背叛......你完全正確!謝謝。 – 2012-09-10 12:48:27