如何将Json数据保存在数据库中

时间:2019-05-23 02:10:08

标签: javascript django

我使用django,可以将数据保存在数据库中,但是如何将javascript变量保存到django数据库中?

我使用django,html,sqlite

models.py

from django.db import models
from jsonfield import JSONField

class jobPlan (models.Model) :
    name = models.CharField(max_length=100, blank=True, null=True)
    position = models.CharField(max_length=100, blank=True, null=True)
    graph = JSONField(default={}, dump_kwargs={'ensure_ascii': False})

    def __str__(self):
        return self.name

forms.py
from django import forms
from jobPlan.models import *


class jobPlanForm(forms.ModelForm):
    class Meta:
        model = jobPlan
        fields = ('graph',)

urls.py

from django.conf.urls import url
from django.conf.urls.static import static
from django.conf import settings
from jobPlan.views import *

urlpatterns = [
    #url('$', ModelingView_model.as_view()),
    url('', jobPlan_view.as_view()),

]

views.py
from django.views.generic.edit import FormView
from jobPlan.forms import *
from django.shortcuts import render, redirect
class jobPlan_view(FormView):
    form_class =jobPlanForm   
    model = jobPlan
    fields = '__all__'
    template_name = "jobPlan.html"
    def post(self, request, *args, **kwargs):
        form = jobPlanForm(request.POST, request.FILES, auto_id=False)
        if form.is_valid():
            form.save()
            return redirect(self.success_url)
        else:
            return render(request, self.template_name, {'form': form})

jobPlan.html

<script>
temp=JSON.stringify(diagrambuilder.toJSON())
</script>

发生任何事件时,我想将temp变量保存在Django数据库中。

0 个答案:

没有答案