带有 pickle 集的 Django 管理界面
我有一个带有一组腌制字符串的模型。 (它必须被腌制,因为 Django 没有内置的设置字段,对吧?)
class Foo(models.Model):
__bar = models.TextField(default=lambda: cPickle.dumps(set()), primary_key=True)
def get_bar(self):
return cPickle.loads(str(self.__bar))
def set_bar(self, values):
self.__bar = cPickle.dumps(values)
bar = property(get_bar, set_bar)
我希望该设置可以在管理界面中编辑。显然,用户不会直接使用腌制的字符串。此外,该界面还需要一个用于在集合中添加/删除字符串的小部件。
这样做的最佳方法是什么?我对 Django 的管理系统不是很熟悉。我需要构建自定义管理小部件或其他东西吗?
更新:如果我确实需要自定义小部件,这看起来很有帮助:http:// www.fictitiousnonsense.com/archives/22
更新 2:现在我正在研究不同的关系模型,看看这是否可行。我正在考虑的一个想法:
class FooMember(models.Model):
name = models.CharField(max_length=120)
foo = models.ForeignKey('Foo')
class Foo(models.Model):
def get_names(self):
return FooMember.objects.filter(foo__exact=self)
这种方法的缺点包括:
- 为一个数据字段(
name
)制作整个模型感觉有点过分。 - 我希望 Foo 的管理界面允许用户输入字符串列表。我不知道如何使用此设置来做到这一点;制作自定义表单小部件似乎工作量更少。
I have a model that has a pickled set of strings. (It has to be pickled, because Django has no built in set field, right?)
class Foo(models.Model):
__bar = models.TextField(default=lambda: cPickle.dumps(set()), primary_key=True)
def get_bar(self):
return cPickle.loads(str(self.__bar))
def set_bar(self, values):
self.__bar = cPickle.dumps(values)
bar = property(get_bar, set_bar)
I would like the set to be editable in the admin interface. Obviously the user won't be working with the pickled string directly. Also, the interface would need a widget for adding/removing strings from a set.
What is the best way to go about doing this? I'm not super familiar with Django's admin system. Do I need to build a custom admin widget or something?
Update: If I do need a custom widget, this looks helpful: http://www.fictitiousnonsense.com/archives/22
Update 2: Now I'm looking through different relational models to see if that will work. One idea I'm toying with:
class FooMember(models.Model):
name = models.CharField(max_length=120)
foo = models.ForeignKey('Foo')
class Foo(models.Model):
def get_names(self):
return FooMember.objects.filter(foo__exact=self)
Disadvantages of this include:
- It feels excessive to make an entire model for one data field (
name
). - I would like the admin interface for
Foo
to allow the user to enter a list of strings. I'm not sure how to do that with this setup; making a custom form widget seems like less work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯。 Django 通常将其数据存储在 SQL 数据库中。将集合存储为 pickled 字符串绝对不是使用 SQL 数据库的最佳方法。对于您的情况,哪种解决方案是正确的解决方案并不是立即显而易见的,这取决于该集合中的内容,但无论如何这都是错误的解决方案。
您可能需要为该集创建一个新表,或者至少将其保存为逗号分隔值或其他内容。
Uhm. Django usually stores it's data in an SQL database. Storing a set as a pickled string is definietly not the best way to use an SQL database. It's not immediately obvious which is the right solution in your case, that depends what is in that set, but this is the wrong solution in any case.
You might want a new table for that set, or at least save it as comma separated values or something.