0
class ArticleRevisionResource(ModelResource):

    article_id = fields.IntegerField(attribute='article_id')

    class Meta:
        queryset = ArticleRevision.objects.all()
        resource_name = 'articlerevision'
        authentication = Authentication()
        authorization = DjangoAuthorization()
        allowed_methods = ['get', 'post', 'put', 'delete']


class ArticleResource(ModelResource):
    #revision = fields.ForeignKey(ArticleRevisionResource, 'article')
    class Meta:
        queryset = Article.objects.all()
        resource_name = 'article'
        authentication = Authentication()
        authorization = DjangoAuthorization()
        allowed_methods = ['get', 'post', 'option', 'put']
        fields = ['title', 'content', 'article_id']

    def obj_create(self, bundle, request=None, **kwargs):
        print("==========Obj_create method==========")
        title, content = bundle.data['title'], bundle.data['content']
        bundle.obj = ArticleRevision.objects.create(title="lala", content="lulu", article_id=40)
        bundle.obj.save()
        return bundle

所以问题是它没有打印出Obj_create方法,但是如果我将相同的调试打印元素放入Meta class,它会打印出来。那么我该怎么做才能找出obj_create方法不起作用的原因呢?

4

2 回答 2

0

你能试着像这样保存它吗

def obj_create(self, bundle, request=None, **kwargs):
    print("==========Obj_create method==========")
    title, content = bundle.data['title'], bundle.data['content']
    bundle.obj = ArticleRevision(title="lala", content="lulu", article_id=40)
    bundle.obj.save()
    return bundle
于 2013-11-08T10:57:37.317 回答
0

在 sweetpie 或 modelresources 中覆盖 obj_create 的正常方法如下:

def obj_create(self, bundle, request=None, **kwargs):
    bundle = super(BookingResource, self).obj_create(bundle)
    bundle.obj.save()
    return bundle
于 2017-04-29T06:18:22.207 回答