Django 1.9 - JSONField in Models

后端 未结 6 624
天命终不由人
天命终不由人 2020-12-30 02:16

I\'m trying to set up a models file in Django 1.9 using the new JSONField. I have found examples using postgres but none with MySql. In the examples with postgres they do

6条回答
  •  长情又很酷
    2020-12-30 02:47

    Try to save data of this model in postgres db on my local machine:

    models.py:

    from django.db import models
    from django import forms
     
    from inputData.models import Input
     
    from django.contrib.postgres.fields import JSONField
     
    class Results(models.Model):
     
        generator = models.OneToOneField(Input, on_delete = models.CASCADE, primary_key = True)
     
        pvalues = JSONField()
    

    views.py

    def result(req, res_id):
        try:
            inp = Input.objects.get(pk = res_id)
            path = os.path.join(BASE_DIR, 'uploads\\' + str(res_id) + '\\t.txt')
            p_values = parse_res.main(path) 
            res = Results(generator = inp, pvalues = p_values)
            res.save(using = 'results')
        except Results.DoesNotExist:
            raise Http404
        return render(req, 'result.html', {'res': res})
    

    settings.py

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        },
        'results': {
            'ENGINE':'django.db.backends.postgresql',
            'NAME': 'results',
            'PASSWORD': 'password',
            'USER': 'user',
            'HOST': '127.0.0.1',
            'PORT': '8000'
    
        }
        
    }
    

    Model Results (see models.py) uses JSONField, which have about 200 bytes of data But at the line res.save(... of code views.py browser does response too long.

    Whatäs wrong with JSON? What problems can be on server besides cache?

提交回复
热议问题