tastypie 和 x-editable 使用补丁

tastypie and x-editable using patch

由于 Whosebug 上的各种其他答案,我几乎 x-editable 使用用 tastypie 构建的 django API,但不完全是。

这里是 html:

 <a href="#" id="field_name" class="editable"
                                 data-name="name"
                                 data-pk="{{ object.id }}"
                                 data-value="{{ object.name }}"
                                 data-title="Meeting Name">
                            {{ object.name }}</a>

和 javascript:

   $('.editable').on('init', function(e, edt) {
        edt.options.url = '/api/v1/update_meeting/' +edt.options.pk;
    });
    $('.editable').editable({
        mode: 'inline',
        ajaxOptions: {
            type: 'PATCH'
        },


        success: function(response, newValue) {

            // nothing to do
        }

    });

还有 tastypie 资源:

class 更新会议资源(模型资源):

class Meta:
    queryset = Meeting.objects.all()
    resource_name = 'update_meeting'
    limit = 0
    include_resource_uri = False
    list_allowed_methods = ['get','patch',]
    detail_allowed_methods = ['get', 'patch']
    serializer = urlencodeSerializer()
    authentication = Authentication()
    authorization = Authorization()

我唯一的问题是字段名称更新为 "name" 而不是 data-value 中的值。在我放入 data-name 属性之前,它将值设置为 "field_name".

我可以通过简单地更改我的 tastypie 资源中的 patch-detail 方法来解决这个问题,但如果不这样做就可以让它正常工作。

所以问题是发送的补丁是键值对,tastypie 无法识别它,但通过快速修改 hydrate 可以修复它。也许不是最好的解决方案,但这是对我有用的:

html:

    <a href="#" id="name" class="editable editable_default_setup"
      data-title="Name"
      data-pk="{{ object.pk }}"
      data-value="{{ object.name }}"
      data-placeholder="Meeting Name" >
      {{ object.name }}</a>

javascript:

$('.editable').on('init', function(e, edt) {
    edt.options.url = '/api/v1/meeting/' + {{ object.id }};
});

$.fn.editable.defaults.ajaxOptions = {type: "PATCH"};

$('.editable_default_setup').editable();

正在设置资源:

 class Meta:
    queryset = Meeting.objects.all()
    include_resource_uri = False
    resource_name = 'meeting'
    limit = 100
    allowed_methods = ['post','put','get','options','patch', 'delete']
    detail_allowed_methods = ['patch']
    authentication = Authentication()
    authorization = Authorization()
    serializer = urlencodeSerializer()
    always_return_data=True

重要的一点,对tastypie资源的修改:

def hydrate(self, bundle):

    if bundle.request.method == "PATCH":
        # data is supplied by x-editable in format {u'pk': u'1170', u'name': u'owner', u'value': u'5', u'format': u'json'}
        # apply this value to the obj and return

        field_name = bundle.data['name']
        field_value = bundle.data['value']

        # the use of name is unfortunate as it can override a field called name, so put it back to original value unless updating it
        # do the same with value, just in case

        bundle.data['name'] = getattr(bundle.obj, 'name', None)
        bundle.data['value'] = getattr(bundle.obj, 'value', None)

        # now set the attribute field_name to field_value so object will update
        bundle.data[field_name] = field_value
        setattr(bundle.obj, field_name, field_value)


 return bundle