带有腌制集的Django Admin界面 [英] Django Admin interface with pickled set

查看:56
本文介绍了带有腌制集的Django Admin界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,该模型具有一组腌制的字符串。 (必须腌制,因为Django没有内置的设置字段,对吗?)

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.

执行此操作的最佳方法是什么?我对Django的管理系统不太熟悉。我是否需要构建自定义管理窗口小部件或其他内容?

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?

更新:如果我确实需要自定义窗口小部件,这似乎很有帮助: http://www.fictitiousnonsense.com/archives/22

Update: If I do need a custom widget, this looks helpful: http://www.fictitiousnonsense.com/archives/22

更新2 :现在,我正在研究不同的关系模型,以了解是否可行。我想玩的一个主意是:

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)

此方法的缺点包括:


  • 为一个数据字段(名称)建立一个完整的模型感觉很过分。

  • 我希望 Foo 的管理界面允许用户输入字符串列表。我不确定如何使用此设置进行操作;

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

推荐答案

Uhm。 Django通常将其数据存储在SQL数据库中。绝对将集合存储为腌制字符串不是使用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.

这篇关于带有腌制集的Django Admin界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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