Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用面片编辑tastypie和x-editable_Javascript_Django_Tastypie_X Editable - Fatal编程技术网

Javascript 使用面片编辑tastypie和x-editable

Javascript 使用面片编辑tastypie和x-editable,javascript,django,tastypie,x-editable,Javascript,Django,Tastypie,X Editable,多亏了stackoverflow上的各种其他答案,我几乎可以用x-editable处理用tastypie构建的django API,但还不完全如此 以下是html: <a href="#" id="field_name" class="editable" data-name="name" data-pk="{{ object.id }}"

多亏了stackoverflow上的各种其他答案,我几乎可以用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>
以及tastypie资源:

 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
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
类UpdateMeetingResourceModelResource:

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 name属性之前,它将值设置为field_name


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

所以问题在于发送的补丁是一个键值对,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>
设置资源:

 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
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
重要的一点是对tastypie资源的修改:

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

通过对补丁细节进行子类化,暂时解决了这个问题。有一个更好的解决方案,并将文件在这里当我有它的工作。。。