【问题标题】:Django tastypie , how to access the object created by post_list in ModelResource classDjango sweetpie,如何访问 ModelResource 类中 post_list 创建的对象
【发布时间】:2015-01-02 20:38:31
【问题描述】:

这是我的美味代码 sn-p。

class MycustomeResource(ModelResource):
    asdf = fields.IntegerField('asdf', null=True)
    adsfasd = fields.IntegerField('adsfasd')

    class Meta:
        always_return_data = True
        queryset = Mycustome.objects.all()


    def post_list(self, request, **kwargs):

        object_temp = super(MycustomeResource, self).post_list(request, **kwargs)

        return object_temp

这里post_list正在创建对象,我想要那个对象,这样我就可以对它执行操作了。

我是否需要覆盖obj_create 才能获取对象..?

【问题讨论】:

    标签: django python-2.7 tastypie


    【解决方案1】:

    是的,post_list 方法调用 ModelResource 类的 obj_create 方法,你可以像这样访问你里面的对象:

    def obj_create(self, bundle, request=None, **kwargs):
        bundle = super(MycustomeResource, self).obj_create(bundle, request)
    
        '''
        operations with your object bundle.obj
        '''
        return bundle
    

    【讨论】:

    • 好的,谢谢,这个obj_create函数在哪里写?
    • 在你的modelResource中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多