如何更改 django-piston 中的 xml 元素名称?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须编写自己的 XMLEmitter。这里总是使用标签
product
而不是resource
。使其智能化需要做更多的工作,因为模型在 Emitter.construct() 方法中序列化为字典,并且不可能正确扩展。如果知道
_to_xml()
方法中的原始模型类并根据类名命名元素,那就太好了。另外,您可能想查看 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 ofresource
.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.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.
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).