如何更改 django-piston 中的 xml 元素名称?

发布于 2024-12-19 06:54:12 字数 966 浏览 1 评论 0原文

我是 django-piston 的新手,每当我获取/发布数据到 xml 时,xml 的元素总是 且<资源>

<response>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-11-30</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>4</resource>
 <resource>2011-12-01</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-12-02</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-12-03</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-12-04</resource>
</resource>
</response>

有没有办法在 handlers.py 中更改它? 我只是想将资源更改为产品,如果可能的话,我可以在 xml 元素中添加一个 id 吗?

I'm new with django-piston and whenever i get/post data to xml, the xml's element is always
and < resource >

<response>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-11-30</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>4</resource>
 <resource>2011-12-01</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-12-02</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-12-03</resource>
</resource>
<resource>
 <resource>4</resource>
 <resource>0</resource>
 <resource>2011-12-04</resource>
</resource>
</response>

is there a way to change it in the handlers.py?
i just want to chage the resource to product and if it possible, can i also put an id to the xml element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无妨# 2024-12-26 06:54:12

您必须编写自己的 XMLEmitter。这里总是使用标签 product 而不是 resource

使其智能化需要做更多的工作,因为模型在 Emitter.construct() 方法中序列化为字典,并且不可能正确扩展。如果知道 _to_xml() 方法中的原始模型类并根据类名命名元素,那就太好了。

from piston.emitters import Emitter, XMLEmitter

class ProductXMLEmitter(XMLEmitter):
    def _to_xml(self, xml, data):
        if isinstance(data, (list, tuple)):
            for item in data:
                attrs = {}
                # if item contains id value, use it as an attribute instead
                if isinstance(item, dict):
                    attrs["id"] = unicode(item.pop("id"))
                xml.startElement("product", attrs)
                self._to_xml(xml, item)
                xml.endElement("product")
        else:
            super(BetterXMLEmitter, self)._to_xml(xml, data)

# replace default XMLEmitter with ours
Emitter.register('xml', ProductXMLEmitter, 'text/xml; charset=utf-8')

另外,您可能想查看 PBS Education 的 django-piston 分支 https://github.com/ PBS-education/django-piston。它允许您使用 PistonViews 以其他方式自定义输出。

You have to write your own XMLEmitter. Here's one that always uses the tag product instead of resource.

Making it intelligent requires a bit more work, since models are serialized into dicts in Emitter.construct() method and it's impossible to extend properly. It would be nice to know the original model class in the _to_xml() method and name the element based on the class name.

from piston.emitters import Emitter, XMLEmitter

class ProductXMLEmitter(XMLEmitter):
    def _to_xml(self, xml, data):
        if isinstance(data, (list, tuple)):
            for item in data:
                attrs = {}
                # if item contains id value, use it as an attribute instead
                if isinstance(item, dict):
                    attrs["id"] = unicode(item.pop("id"))
                xml.startElement("product", attrs)
                self._to_xml(xml, item)
                xml.endElement("product")
        else:
            super(BetterXMLEmitter, self)._to_xml(xml, data)

# replace default XMLEmitter with ours
Emitter.register('xml', ProductXMLEmitter, 'text/xml; charset=utf-8')

Also, you may want to look at PBS Education's django-piston fork at https://github.com/pbs-education/django-piston. It allows you to customize the output in other ways with PistonViews.

蓝天 2024-12-26 06:54:12

Elementtree 将为您提供帮助。您可以更改您想要更改的任何内容,读取文件,使用 elementree 解析它并更新值,然后再次将其放入文件中(如果需要)。

Elementtree will help you. You can change whatever you want to change, Read the file, parse it using elementree and update the values and again put it to the file (if needed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文