fields.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:mes 作者: osess 项目源码 文件源码
def resource_from_data(self, fk_resource, data, request=None, related_obj=None, related_name=None):
        """
        Given a dictionary-like structure is provided, a fresh related
        resource is created using that data.
        """
        # Try to hydrate the data provided.
        data = dict_strip_unicode_keys(data)
        fk_bundle = fk_resource.build_bundle(
            data=data,
            request=request
        )

        if related_obj:
            fk_bundle.related_obj = related_obj
            fk_bundle.related_name = related_name

        unique_keys = dict((k, v) for k, v in data.iteritems() if k == 'pk' or (hasattr(fk_resource, k) and getattr(fk_resource, k).unique))

        # If we have no unique keys, we shouldn't go look for some resource that 
        # happens to match other kwargs. In the case of a create, it might be the
        # completely wrong resource.
        # We also need to check to see if updates are allowed on the FK resource. 
        if unique_keys and fk_resource.can_update():            
            try:
                return fk_resource.obj_update(fk_bundle, skip_errors=True, **data)
            except (NotFound, TypeError):
                try:
                    # Attempt lookup by primary key
                    return fk_resource.obj_update(fk_bundle, skip_errors=True, **unique_keys)
                except NotFound:
                    pass
            except MultipleObjectsReturned:
                pass

        # If we shouldn't update a resource, or we couldn't find a matching
        # resource we'll just return a populated bundle instead
        # of mistakenly updating something that should be read-only.
        fk_bundle = fk_resource.full_hydrate(fk_bundle)
        fk_resource.is_valid(fk_bundle)
        return fk_bundle
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号