我到底该如何使用HStoreField? [英] How exactly do I have to use HStoreField?

查看:90
本文介绍了我到底该如何使用HStoreField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the latest version of Django and DRF. But after running it I'm getting this error:

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

class HStoreDescriptor(models.fields.subclassing.Creator): AttributeError: module 'django.db.models.fields' has no attribute 'subclassing'

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

I'm not sure how to use HStoreField and create an extension using migration. This is my structure of the file.

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/

Models.py

Models.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)

serializers.py

serializers.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_'

请告诉我我可以做什么?

Please tell what changes I can do? If you want more information please do ask.

推荐答案


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

  2. 连接Postgresql并创建hstore扩展名:

  1. Add 'django.contrib.postgres' in your INSTALLED_APPS.
  2. Connect Postgresql and create hstore extension:

sudo su-postgres
\c数据库;
如果不存在,则创建扩展hstore;

在您的模型中。py:

从django.contrib.postgres.fields导入HStoreField
类Section(models.Model):
title = models.CharField(max_length = 256)
参数= HStoreField(blank = True,null = True)

在终端上运行以下命令:

Run following commands on the terminal:

python manage.py makemigrations
python manage.py migration

默认情况下,它为您提供了难看的文本区域来编写json键/值对。如果您喜欢精美的hstore小部件,则可以安装它:

By default, it gives you ugly text-area to write json key-value pairs. If you like a fancy hstore widget, then you can install it:


  1. pip install django-admin-hstore-小部件

  2. 将django_admin_hstore_widget添加到您的INSTALLED_APPS(在settings.py中)

  3. 然后在您应用的admin.py中,将其添加。

from your_app.models import Section

from your_app.models import Section

from django从django_admin_hstore_widget.forms导入表单

from django import forms



from django_admin_hstore_widget.forms import HStoreFormField

class SectionAdminForm(forms.ModelForm):
    parameters= HStoreFormField()

    class Meta:
        model = Section
        exclude = ()


@admin.register(Section)
class SectionAdmin(admin.ModelAdmin):
    form = SectionAdminForm

这篇关于我到底该如何使用HStoreField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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