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: module 'django.db.models.fields' has no attribute 'subclassing'
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
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_'
Please tell what changes I can do? If you want more information please do ask.
'django.contrib.postgres'
in your INSTALLED_APPS.Connect Postgresql and create hstore extension:
sudo su - postgres \c database; CREATE EXTENSION IF NOT EXISTS hstore;
In your 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)
Run following commands on the terminal:
python manage.py makemigrations python manage.py migrate
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:
pip install django-admin-hstore-widget
.from your_app.models import Section
from django import forms
from django_admin_hstore_widget.forms import HStoreFormField