如何使用django-tastypie为继承另一个模型的Model创建ModelResource?

时间:2012-07-18 14:12:42

标签: python django tastypie

我的django模型看起来像:

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属性。好。因此,我为ModelResourceSession创建了Invoice,想象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模型是一个抽象类。

1 个答案:

答案 0 :(得分:2)

我认为您必须忘记设置URL SessionResource。

from tastypie.api import Api

api = Api()

api.register(SessionResource())

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

你是在urls.py吗?

拥抱。