Commit 31fd8de1 authored by alexandre's avatar alexandre

Changed models and DB to use PostrgreSQL JSONField

parent fc541e14
local_settings.py
# If you need to exclude files such as those generated by an IDE, use
# $GIT_DIR/info/exclude or the core.excludesFile configuration variable as
# described in https://git-scm.com/docs/gitignore
......
......@@ -19,8 +19,12 @@ ALLOWED_HOSTS = []
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
......
LOCAL_SETTINGS = True
from .settings import *
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_gs%q+2!)n#^33k45wie13vtxj7yv)zu8zhekz)p*i4gs*p*8%'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
......@@ -21,7 +21,6 @@ from playground import views
router = routers.DefaultRouter()
router.register(r'axis', views.AxisViewSet)
router.register(r'scores', views.ScoreViewSet)
......
from django.contrib import admin
from .models import Axis, Score
class AxisAdmin(admin.ModelAdmin):
pass
from .models import Score
class ScoreAdmin(admin.ModelAdmin):
pass
admin.site.register(Axis, AxisAdmin)
admin.site.register(Score, ScoreAdmin)
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-05-22 13:34
from __future__ import unicode_literals
import django.contrib.postgres.fields.jsonb
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('playground', '0004_auto_20170502_1009'),
]
operations = [
migrations.AddField(
model_name='score',
name='body',
field=django.contrib.postgres.fields.jsonb.JSONField(default={}),
preserve_default=False,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-05-22 14:04
from __future__ import unicode_literals
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('playground', '0005_score_body'),
]
operations = [
migrations.RemoveField(
model_name='axis',
name='children',
),
migrations.RemoveField(
model_name='score',
name='body',
),
migrations.RemoveField(
model_name='score',
name='child',
),
migrations.AddField(
model_name='score',
name='mainline',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default='{}'),
preserve_default=False,
),
migrations.AddField(
model_name='score',
name='stage_set',
field=models.TextField(blank=True),
),
migrations.DeleteModel(
name='Axis',
),
]
from django.db import models
class Axis(models.Model):
"""docstring"""
title = models.TextField(blank=True)
children = models.ManyToManyField("Axis", blank=True, null=True)
def __str__(self):
return self.title
from django.contrib.postgres.fields import JSONField
class Score(models.Model):
"""docstring"""
title = models.TextField(blank=True)
child = models.ForeignKey(Axis, blank=True, null=True)
stage_set = models.TextField(blank=True)
mainline = JSONField(blank=True)
def __str__(self):
return self.title
from .models import Axis, Score
from .models import Score
from rest_framework import serializers
from rest_framework_recursive.fields import RecursiveField
class AxisSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
children = RecursiveField(required=False, allow_null=True, many=True)
class Meta:
model = Axis
fields = ('id', 'title', 'children')
class ScoreSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
child = AxisSerializer()
class Meta:
model = Score
fields = ('id', 'title', 'child')
fields = ('id', 'title', 'stage_set', 'mainline')
This diff is collapsed.
......@@ -13,7 +13,7 @@
<script id="score-template" type="text/template">
<header>
<h1><%- title %></h1>
<div><%= mise %></div>
<div><%= stage_set %></div>
</header>
<ol></ol>
</script>
......@@ -53,7 +53,7 @@
</span>
</div>
<% if (children.length) { %>
<% if (sublines.length) { %>
<ol></ol>
<% } %>
</script>
......
from django.views.generic import DetailView
from django.views.generic.base import TemplateView
from rest_framework import viewsets
from .models import Axis, Score
from .serializers import AxisSerializer, ScoreSerializer
from .models import Score
from .serializers import ScoreSerializer
class ScoreViewSet(viewsets.ModelViewSet):
......@@ -13,14 +13,6 @@ class ScoreViewSet(viewsets.ModelViewSet):
serializer_class = ScoreSerializer
class AxisViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Axis.objects.all()
serializer_class = AxisSerializer
class ScoreDetailView(DetailView):
"""
"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment