Django管理界面将字符u“[]添加到带有SeparatedValuesField的字符串列表中 [英] Django admin interface adds characters u"[ ] to a list of strings with SeparatedValuesField

查看:262
本文介绍了Django管理界面将字符u“[]添加到带有SeparatedValuesField的字符串列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SeparatedValuesField跟踪一个字符串列表,如 http://davidcramer.posterous.com/code/181/custom-fields-in-django.html 和许多在这里的帖子,推荐这个作为存储字符串列表的正确方法

I'm using SeparatedValuesField to keep track of a list of strings as explained by http://davidcramer.posterous.com/code/181/custom-fields-in-django.html and many posts here on SO that recommend this as the right way to store a list of strings.

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    device_ids = SeparatedValuesField(blank=True, null=True, default=[])

它正常工作我的应用程序,我可以添加设备ID,并按照预期在管理界面中查看。

It is working fine in my application, I can add device ids and view them in the admin interface as expected.

user_profile.device_ids = ['666666-D849-524F-6984-7E9B2D768546']

但问题出在管理界面,当我打开在UserProfile对象的详细页面上,管理界面本身正在为我的device_ids字段添加额外的值。

But the problem is in the admin interface, when I open up the detail page for a UserProfile object, the admin interface itself is adding extra values to my device_ids field.

对于e例如,我的应用程序在该字段中插入一个值,当我在管理界面中查看它时,如下所示:

For example, my application inserts a value into the field, and when I view it in the admin interface it looks like this:

[u'666666-D849-524F-6984-7E9B2D768546']

然后我更改一些其他随机属性我的UserProfile对象并使用内置的django管理界面保存按钮进行保存。

Then I change some other random property on my UserProfile object and save it using the built in django admin interface save button.

当我为对象再次打开UserProfile对象详细信息页面时,该值现在看起来像这个:

When I open the UserProfile object detail page up again for my object, that value now looks like this:

[u"[u'666666-D849-524F-6984-7E9B2D768546']"]

如果我重复这个过程,只需点击保存,然后打开这个详细页面,它将继续嵌套实际值与u []字符。

If I repeat this process of just hitting save then opening up this detail page, it will continue nesting the actual value with u"[ ] characters.

我可以做些什么来改变这个功能?我应该以不同的方式存储字符串列表吗?

Is there something I can do to change this functionality? Should I be storing the list of strings in a different way?

推荐答案

Oiva Eskola的答案看起来不错(没有尝试过, )

Oiva Eskola's answer looks fine (haven't tried it, though).

在此问题之前查找Django源代码,我也偶然发现了以下内容(作品):

Finding the Django source before this question, I also stumbled upon the following (which works):

SeparatedValuesField(models.TextField):
    # ...

    def value_from_object(self, obj):
        return self.get_db_prep_value(super(SeparatedValuesField, self).value_from_object(obj))

这篇关于Django管理界面将字符u“[]添加到带有SeparatedValuesField的字符串列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆