【问题标题】:Django-hstore and admin widget: way to unsort hstore dataDjango-hstore 和管理小部件:对 hstore 数据进行排序的方法
【发布时间】:2017-02-12 11:15:48
【问题描述】:

我正在使用 Django-hstore 库,并且有漂亮的管理小部件。主题表存储计算机的组件,如下所示:

class Component(models.Model):

    name = models.CharField(max_length=64)
    purchase_date = models.DateField(blank=True, null=True)
    product_page = models.URLField(blank=True, help_text='url to pruduct page')
    <...>
    data = hstore.DictionaryField(blank=True)

    def load_cpu_data(self):
        if self.product_page:
            info = cpu_data(self.product_page)
            if info:  # info is a SortedDict with proper order
                for key, value in info.items():
                    self.data[key] = value
                self.save()

接下来,我从cpu-world.com 获取有关必要 CPU 的数据,并且我在管理员中有以下内联数据:

看起来不错,但按字母顺序排序而不是逻辑排序,以便在load_cpu_data 模型方法中将数据加载到数据库中。正确顺序的示例,例如在 cpu-world 上:

Family
Model number
Frequency
Socket
Microarchitecture
Processor core
Manufacturing process
Data width
The number of CPU cores
The number of threads
Integrated graphics
Thermal Design Power

是否有技术、技巧或其他东西可以帮助我以所需的顺序显示数据?例如,我找到了 python OrderedDict 数据类型,这与我需要的相似。但是显然hstore内部结构乱七八糟的数据顺序。

【问题讨论】:

    标签: python django django-admin hstore django-hstore


    【解决方案1】:

    根据the hstore docs, hstore itself is not ordered

    对的顺序并不重要(并且可能不会在输出时重现)。对之间或 => 符号周围的空格将被忽略。包含空格、逗号、=s 或 >s 的双引号键和值。要在键或值中包含双引号或反斜杠,请使用反斜杠对其进行转义。

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 1970-01-01
      • 2014-08-31
      • 2016-12-08
      • 1970-01-01
      • 2017-04-05
      • 2016-01-06
      • 2016-08-15
      • 2012-11-25
      相关资源
      最近更新 更多