2012-07-18 79 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