我正在使用最新版本的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/
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
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_'
请告诉我我可以做什么?如果您需要更多信息,请询问。
答案 0 :(得分:0)
您不需要导入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()
答案 1 :(得分:0)
'django.contrib.postgres'
。连接Postgresql并创建hstore扩展名:
sudo su - postgres
\c database;
CREATE EXTENSION IF NOT EXISTS hstore;
在您的模型中。py:
from django.contrib.postgres.fields import HStoreField
class Section(models.Model):
title = models.CharField(max_length=256)
parameters = HStoreField(blank=True,null=True)
在终端上运行以下命令:
python manage.py makemigrations
python manage.py migrate
默认情况下,它使您在丑陋的文本区域中编写JSON键值对。如果您喜欢精美的hstore小部件,则可以安装它:
pip install django-admin-hstore-widget
。来自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