django tastypie更新两个型号 [英] django tastypie update two models

查看:157
本文介绍了django tastypie更新两个型号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,一个用户模型和一个候选模型,它有两个模型,一个(POST)api调用的两个模型的更新。引用用户模型。我们想通过api界面发布候选模型,但想隐藏用户模型。因此,作为第一步,我将用户模型字段与脱水过程中的候选模型字段合并。这个工作完全正常。



问题是,我无法弄清楚,如何做到这一点(水合和创建两个模型,我们需要创建一个单独的用户模型,不能合并两个模型)

解决方案

如果您向我们展示了一些代码,你尝试过,但是对于这种任务,你应该覆盖 obj_create(...)方法 tastypie.resources.ModelResource class。



看起来像这样:

  def obj_create(self,bundle,request = None,** kwargs):

obj_create的ORM特定实现

.obj = self._meta.object_class()

为key,值在kwargs.items()中:
setattr(bundle.obj,key,value)

bundle = self.full_hydrate(bundle)

#保存FK以防万一。
self.save_related(bundle)

#保存主对象。
bundle.obj.save()

#现在拿起M2M位。
m2m_bundle = self.hydrate_m2m(bundle)
self.save_m2m(m2m_bundle)
返回包

所以在您的资源中,您可以使用以下内容:

  from tastypie.resources import ModelResource 

class MyResource(ModelResource):

def obj_create(self,bundle,request = None,** kwargs):
#...
#create User实例基于捆绑
#user = ...
#...
#kwargs ['user'] = user<将被设置在super()
#...

#call super的Candidate实例中,导致创建候选模型
super(MyResource,self).obj_create(自我,束缚,请求,** kwargs)

这应该让你开始。如果您有任何问题,请提出问题并提供一些代码。


I have a problem with tastypie regarding updates to two models with one (POST) api call.

We have two models, an user model and a candidate model which references the user model. We want to publish the candidate model via the api interface, but want to hide the user model. So, as a first step I merge the user model fields with the candidate model fields in the dehydrate process. This is working completly fine.

The problem is, that I can't figure out, how to do it the other way round (hydrate and create both models. we need to create a seperate user model and cant just merge both models)

解决方案

Would be nice if you showed us some code and what have you tried, but for this kind of task you should probably override the obj_create(...) method of tastypie.resources.ModelResource class.

It looks like this:

    def obj_create(self, bundle, request=None, **kwargs):
        """
        A ORM-specific implementation of ``obj_create``.
        """
        bundle.obj = self._meta.object_class()

        for key, value in kwargs.items():
            setattr(bundle.obj, key, value)

        bundle = self.full_hydrate(bundle)

        # Save FKs just in case.
        self.save_related(bundle)

        # Save the main object.
        bundle.obj.save()

        # Now pick up the M2M bits.
        m2m_bundle = self.hydrate_m2m(bundle)
        self.save_m2m(m2m_bundle)
        return bundle

So in your resource you could have something like:

from tastypie.resources import ModelResource

class MyResource( ModelResource ):

    def obj_create( self, bundle, request = None, **kwargs ):
        # ...
        # create User instance based on what's in the bundle
        # user = ...
        # ...
        # kwargs[ 'user' ] = user < will be set on Candidate instance in super()
        # ...

        # call super, resulting in creation of the Candidate model
        super( MyResource, self ).obj_create( self, bundle, request, **kwargs )

And this should get you started. If you have any trouble, please ask a question and provide some code.

这篇关于django tastypie更新两个型号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆