tastypie和x-editable使用补丁

时间:2015-02-18 15:34:49

标签: javascript django tastypie x-editable

由于stackoverflow上的各种其他答案,我几乎可以使用由tastypie构建的django API进行x-editable处理,但并不完全。

这是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资源:

类UpdateMeetingResource(ModelResource):

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()

我唯一的问题是字段名称会更新为&#34; name&#34;而不是数据值的价值。在我输入data-name属性之前,它将值设置为&#34; field_name&#34;。

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

1 个答案:

答案 0 :(得分:0)

所以问题是被发送的补丁是一个关键值对,并且tastypie没有识别,但快速修改水合物修复了它。也许不是最好的解决方案,但这里有什么对我有用:

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