0

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

类 HStoreDescriptor(models.fields.subclassing.Creator): AttributeError: module '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_'

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

4

2 回答 2

1
  1. 添加'django.contrib.postgres'您的INSTALLED_APPS
  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
    

    在此处输入图像描述

于 2019-12-03T08:04:28.817 回答
0

你不需要导入 django_hstore

添加'django.contrib.postgres'您的 INSTALLED_APPS。

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

在模型上:添加:from django.contrib.postgres.fields import HStoreField

添加字段:objects = HStoreField()

文档

于 2019-03-31T04:55:01.330 回答