Tastypie保存外键? (django,backbone.js)

时间:2013-08-21 13:22:53

标签: backbone.js tastypie

我在django中有两个模型

class Book(models.Model):
    name = models.CharField(max_length=200)
class Title(models.Model):
    book = models.ForeignKey(Book)
    titlename = models.CharField(max_length=200)

现在我有以下的tastypie

class TitleResource(ModelResource):
    class Meta:
        queryset = Title.objects.all()
        authentication = Authentication()
        authorization = Authorization()
        filtering = {'id':ALL}

我想在我的标题表中存储一个新条目。所以,我使用以下Backbone模型的保存方法

title.save({titlename: "ABC", book_id: 1})

网址为/api/v1/title/

但它会引发错误:book_id不能为null。 (我在 book 表中有一个条目,其中id为1)。

我需要使用哪种方法?滋润?或obj_create?

1 个答案:

答案 0 :(得分:1)

在这里,我需要写一个 BookResource 并发回{titlename: "123",book:/api/v1/book/1/}

相关问题