【问题标题】:How to create a ModelResource for a Model that inherits another Model using django-tastypie?如何使用 django-tastypie 为继承另一个模型的模型创建模型资源?
【发布时间】:2012-07-18 14:12:41
【问题描述】:

我的 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 属性。好的。所以,我为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 模型是一个抽象类。

【问题讨论】:

    标签: python django tastypie


    【解决方案1】:

    我想你一定是忘记设置 url SessionResource 了。

    from tastypie.api import Api
    
    api = Api()
    
    api.register(SessionResource())
    
    urlpatterns += patterns('',
        (r'^api/', include(api.urls)),
    )
    

    您在 urls.py 中执行此操作?

    拥抱。

    【讨论】:

    • 天哪!被疲劳背叛了……你完全正确!谢谢。
    猜你喜欢
    • 2021-06-18
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多