How exactly do I have to use HStoreField?

3.6k Views Asked by At

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.

2

There are 2 best solutions below

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

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

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

  4. Run following commands on the terminal:

    python manage.py makemigrations python manage.py migrate

  5. 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-widget.
    2. Add django_admin_hstore_widget to your INSTALLED_APPS ( in settings.py )
    3. Then in admin.py of your app, add this.

    from your_app.models import Section

    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
    

    enter image description here

5
On

you do not need to import django_hstore

Add 'django.contrib.postgres' in your INSTALLED_APPS.

If hstrone not enabled on Postgres , Run sql script: CREATE EXTENSION IF NOT EXISTS hstore

On Model: Add: from django.contrib.postgres.fields import HStoreField

Add Field: objects = HStoreField()

docs