【问题标题】:How exactly do I have to use HStoreField?我究竟该如何使用 HStoreField?
【发布时间】:2019-08-21 14:07:54
【问题描述】:

我正在使用最新版本的 Django 和 DRF。但是运行它之后我得到了这个错误:

类 HStoreDescriptor(models.fields.subclassing.Creator): AttributeError: 模块 'django.db.models.fields' 没有属性 'subclassing'

我不确定如何使用 HStoreField 并使用迁移创建扩展。这是我的文件结构。

webhook10/
 |-- tutorial/
 |    |-- slack/                
 |    |    |-- migrations/
 |    |    |    +-- __init__.py
 |    |    |-- __init__.py
 |    |    |-- admin.py
 |    |    |-- apps.py
 |    |    |-- models.py
 |    |    |-- tests.py
 |    |    |-- urls.py
 |    |    +-- views.py
 |    |-- tutorial/
 |    |    |-- __init__.py
 |    |    |-- settings.py
 |    |    |-- urls.py
 |    |    |-- wsgi.py
 |    +-- manage.py
 +-- venv/

模型.py

from django.db import models
from django.utils import timezone
from django_hstore import hstore

class WebhookTransaction(models.Model):
    UNPROCESSED = 1
    PROCESSED = 2
    ERROR = 3

    STATUSES = (
        (UNPROCESSED, 'Unprocessed'),
        (PROCESSED, 'Processed'),
        (ERROR, 'Error'),
    )

    date_generated = models.DateTimeField()
    date_received = models.DateTimeField(default=timezone.now)
    body = hstore.SerializedDictionaryField()
    request_meta = hstore.SerializedDictionaryField()
    status = models.CharField(max_length=250, choices=STATUSES, default=UNPROCESSED)

    objects = hstore.HStoreManager()

    def __unicode__(self):
        return u'{0}'.format(self.date_event_generated)

class Message(models.Model):
    date_processed = models.DateTimeField(default=timezone.now)
    webhook_transaction = models.OneToOneField(WebhookTransaction)

    team_id = models.CharField(max_length=250)
    team_domain = models.CharField(max_length=250)
    channel_id = models.CharField(max_length=250)
    channel_name = models.CharField(max_length=250)
    user_id = models.CharField(max_length=250)
    user_name = models.CharField(max_length=250)
    text = models.TextField()
    trigger_word = models.CharField(max_length=250)

    def __unicode__(self):
        return u'{}'.format(self.user_name)

序列化器.py

from rest_framework import serializers
from slack.models import WebhookTransaction, Message

class WebhookTransactionSerializer(serializers.ModelSerializer):
    class Meta:
        model = WebhookTransaction
        fields = '_all_'

class MessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields = '_all_'

请告诉我可以做哪些更改?如果您想了解更多信息,请询问。

【问题讨论】:

    标签: django django-models django-rest-framework django-hstore


    【解决方案1】:
    1. 在您的INSTALLED_APPS中添加'django.contrib.postgres'
    2. 连接 Postgresql 并创建 hstore 扩展:

      sudo su - postgres \c database; CREATE EXTENSION IF NOT EXISTS hstore;

    3. 在你的 models.py 中:

      from django.contrib.postgres.fields import HStoreField class Section(models.Model): title = models.CharField(max_length=256) parameters = HStoreField(blank=True,null=True)

    4. 在终端上运行以下命令:

      python manage.py makemigrations python manage.py migrate

    5. 默认情况下,它会为您提供丑陋的文本区域来编写 json 键值对。如果你喜欢花哨的 hstore 小部件,那么你可以安装它:

      1. pip install django-admin-hstore-widget
      2. 将 django_admin_hstore_widget 添加到您的 INSTALLED_APPS(在 settings.py 中)
      3. 然后在你的应用的 admin.py 中,添加这个。

      从 your_app.models 导入部分

      从 django 导入表单

      从 django_admin_hstore_widget.forms 导入 HStoreFormField

      class SectionAdminForm(forms.ModelForm):
          parameters= HStoreFormField()
      
          class Meta:
              model = Section
              exclude = ()
      
      
      @admin.register(Section)
      class SectionAdmin(admin.ModelAdmin):
          form = SectionAdminForm
      

    【讨论】:

      【解决方案2】:

      你不需要导入 django_hstore

      在您的 INSTALLED_APPS 中添加 'django.contrib.postgres'

      如果在 Postgres 上没有启用 hstrone,运行 sql 脚本:CREATE EXTENSION IF NOT EXISTS hstore

      型号:添加:from django.contrib.postgres.fields import HStoreField

      添加字段:objects = HStoreField()

      docs

      【讨论】:

      • 如果不存在则创建扩展 hstore 文件“”,第 1 行创建扩展如果不存在 hstore ^
      • CREATE EXTENSION 是 SQL 命令,而不是 python 命令。您可以在数据库 shell 中运行它,直接通过psql 或通过manage.py dbshell(如果配置正确)。
      • @siddhantSingh 要么 psql 要么通过 manage.py dbshel​​l 如 wmorrell 所说
      • 在此之前我是否必须创建超级用户,因为如果我试图保存我的模型,它不会让我运行任何东西,甚至不能运行 manage.py dbshel​​l。 @wmorrell
      • 在配置数据库支持它之前,您不能使用任何模型代码。无论如何,HSTORE 在这一点上实际上已经被 JSONB 弃用了,所以只要你使用 PostgreSQL 9.2 或更高版本,请尝试查看django.contrib.postgres.fields.JSONFielddocs.djangoproject.com/en/2.1/ref/contrib/postgres/fields/…
      猜你喜欢
      • 2011-07-17
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 2014-08-03
      • 1970-01-01
      • 2021-01-07
      • 2021-09-13
      相关资源
      最近更新 更多